While using listbox in c#, how can learn the count of selecteditems?
Listbox items: Denver, Chicago, Baltimora. For example, I select Chicago and Denver.
I want to make a loop in order to assign selecteditems.
How can I achieve it? How can I learn the number of selected item?
I have tried using the code below. But when I select Chicago and Denver the count value is 0...
Thank you
string city = string.Empty;
int count = 0;
foreach (ListItem item in myddl.Items)
{
if (item.Selected)
{
count++;
if(count == 1)
{
city = item.Value.ToString() + ";";
}
else
{
city += item.Value.ToString() + "; ";
}
}
}
The listbox
<asp:ListBox ID="myddl" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="[ === Select your City === ]" Value=""></asp:ListItem>
<asp:ListItem Text="Denver" Value="Denver"></asp:ListItem>
<asp:ListItem Text="Chicago" Value="Chicago"></asp:ListItem>
<asp:ListItem Text="Baltimora" Value="Baltimora"></asp:ListItem>
</asp:ListBox>