ASP:ListBox Get Selected Items - One Liner?
Using LINQ:
string values = String.Join(", ", lbAppGroup.Items.Cast<ListItem>()
.Where(i => i.Selected)
.Select(i => i.Value));
I don't think there is anything built in but you could do something like this:
<asp:ListBox runat="server" ID="listBox" SelectionMode="Multiple">
<asp:ListItem Selected="True" Text="text1" Value="value1"></asp:ListItem>
<asp:ListItem Selected="false" Text="text2" Value="value2"></asp:ListItem>
<asp:ListItem Selected="True" Text="text3" Value="value3"></asp:ListItem>
<asp:ListItem Selected="True" Text="text4" Value="value4"></asp:ListItem>
</asp:ListBox>
IEnumerable<string> selectedValues = from item in listBox.Items.Cast<ListItem>()
where item.Selected
select item.Text;
string s = string.Join(",", selectedValues);
var selectedQuery = listBox.Items.Cast<ListItem>().Where(item => item.Selected);
string selectedItems = String.Join(",", selectedQuery).TrimEnd();