I have an asp:ListBox that I'm dynamically generating items for. 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.