简体   繁体   English

ListBox多个选择模式不起作用

[英]ListBox multiple selection mode not working

I have an asp:ListBox that I'm dynamically generating items for. 我有一个asp:ListBox,我正在为其动态生成项目。 The Selection mode is set to "multiple" but I can only select one item 选择模式设置为“多个”,但我只能选择一项

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

var languages = GetSiteLanguages();
foreach (var lang in languages)
{
    lstLanguages.Items.Add(new ListItem(lang.Name));
}
lstLanguages.SelectionMode = ListSelectionMode.Multiple;

I tried setting the selection mode both in the aspx file and in the code behind but it's not working. 我尝试在aspx文件和后面的代码中都设置选择模式,但是它不起作用。

You are adding Items in a wrong way in the ListBox. 您在列表框中以错误的方式添加项目。 The correct way to add items is: 添加项目的正确方法是:

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

Note: I am supposing that the "languages" is a List of Strings. 注意:我假设“语言”是一个字符串列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM