Created
June 18, 2018 20:52
-
-
Save smetronic/96e187d77de61f31aa567b39169c6a2b to your computer and use it in GitHub Desktop.
Getting all selected values from an ASP ListBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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