Skip to content

Instantly share code, notes, and snippets.

@smetronic
Created June 18, 2018 20:52
Show Gist options
  • Save smetronic/96e187d77de61f31aa567b39169c6a2b to your computer and use it in GitHub Desktop.
Save smetronic/96e187d77de61f31aa567b39169c6a2b to your computer and use it in GitHub Desktop.
Getting all selected values from an ASP ListBox
// GetSelectedIndices
foreach (int i in ListBox1.GetSelectedIndices())
{
// ListBox1.Items[i] ...
}
// Items collection
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
{
// item ...
}
}
// LINQ over Items collection (must cast Items)
var query = from ListItem item in ListBox1.Items where item.Selected select item;
foreach (ListItem item in query)
{
// item ...
}
// LINQ lambda syntax
var query = ListBox1.Items.Cast<ListItem>().Where(item => item.Selected);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment