How to get values of selected items in CheckBoxList with foreach in ASP.NET C#? How to get values of selected items in CheckBoxList with foreach in ASP.NET C#? asp.net asp.net

How to get values of selected items in CheckBoxList with foreach in ASP.NET C#?


Note that I prefer the code to be short.

List<ListItem> selected = CBLGold.Items.Cast<ListItem>()    .Where(li => li.Selected)    .ToList();

or with a simple foreach:

List<ListItem> selected = new List<ListItem>();foreach (ListItem item in CBLGold.Items)    if (item.Selected) selected.Add(item);

If you just want the ListItem.Value:

List<string> selectedValues = CBLGold.Items.Cast<ListItem>()   .Where(li => li.Selected)   .Select(li => li.Value)   .ToList();


foreach (ListItem item in CBLGold.Items){    if (item.Selected)    {        string selectedValue = item.Value;    }}


Good afternoon, you could always use a little LINQ to get the selected list items and then do what you want with the results:

var selected = CBLGold.Items.Cast<ListItem>().Where(x => x.Selected);// work with selected...