字符串为dropdownlist作为项目



在ASP.NET WebForm上,我尝试使用从服务器接收到的项目填充dropdownlist,但它以文字显示为文本。

如何将检索的字符串添加到dropdownlist作为项目?

按钮单击代码:

protected void btnSend_Click1(object sender, EventArgs e)
    {
        server.Connect(ipep);
        NetworkStream stream = new NetworkStream(server);
        Byte[] data = System.Text.Encoding.ASCII.GetBytes("GU");
        stream.Write(data, 0, data.Length);
        data = new Byte[256];
        Int32 bytes = stream.Read(data, 0, data.Length);
        ResponseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
        DropDownList1.Items.Add(ResponseData);
        server.Close();
    }

在另一侧PowerShell Server上将完成功能和响应:

function GU
{
$global:rresult = @("test1" , "test2" , "test3")
}

不幸的是,结果是:下拉列表

尝试使用.Split(',')和其他事情,但没有任何效果。知道如何将"test1" , "test2" , "test3"添加为单独的项目?

看来您的ResponseData最终成为"test1 test2 test3"

现在,您想将其字符串化为单个令牌,我们可以看到您的字符串是由空间界定的。因此,我们可以使用ResponseData.Split(' ')将其归为代币以获取令牌。

ResponseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
string[] tokens = ResponseData.Split(' ');
foreach (string token in tokens)
    DropDownList1.Items.Add(token);

最新更新