Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表框多选模式不工作_C#_Asp.net_Listbox_Listitem - Fatal编程技术网

C# 列表框多选模式不工作

C# 列表框多选模式不工作,c#,asp.net,listbox,listitem,C#,Asp.net,Listbox,Listitem,我有一个asp:ListBox,我正在为它动态生成项目。选择模式设置为“多个”,但我只能选择一个项目 <asp:ListBox runat="server" ID="lstLanguages" SelectionMode="multiple" Width="200"> </asp:ListBox> 我尝试在aspx文件和代码隐藏中设置选择模式,但不起作用。您在列表框中以错误的方式添加项目。 添加项目的正确方法是: var languages = GetSiteL

我有一个asp:ListBox,我正在为它动态生成项目。选择模式设置为“多个”,但我只能选择一个项目

<asp:ListBox runat="server" ID="lstLanguages" SelectionMode="multiple" Width="200">
</asp:ListBox>

我尝试在aspx文件和代码隐藏中设置选择模式,但不起作用。

您在列表框中以错误的方式添加项目。 添加项目的正确方法是:

    var languages = GetSiteLanguages();
    foreach (var lang in languages)
    {
        lstLanguages.Items.Add(lang); //lang should be a string value.
    }

注意:我假设“语言”是一个字符串列表。

我们无法重现上述代码的问题。你能创建吗?这将是一个愚蠢的问题,可能很粗鲁,但你是否尝试过使用shift或control键,甚至拖动鼠标来突出显示项目?因为你的两个代码都应该工作。哈,可能就是这样。PEBKACAdd方法有两个重载。任何一种方法都是正确的。
    var languages = GetSiteLanguages();
    foreach (var lang in languages)
    {
        lstLanguages.Items.Add(lang); //lang should be a string value.
    }