How to loop through a checkboxlist and to find what's checked and not checked?

Bill Martin picture Bill Martin · Dec 27, 2008 · Viewed 136.5k times · Source

I'm trying to loop through items of a checkbox list. If it's checked, I want to set a value. If not, I want to set another value. I was using the below, but it only gives me checked items:

foreach (DataRowView myRow in clbIncludes.CheckedItems)
{
    MarkVehicle(myRow);
}

Answer

Contra picture Contra · Sep 27, 2010

This will give a list of selected

List<ListItem> items =  checkboxlist.Items.Cast<ListItem>().Where(n => n.Selected).ToList();

This will give a list of the selected boxes' values (change Value for Text if that is wanted):

var values =  checkboxlist.Items.Cast<ListItem>().Where(n => n.Selected).Select(n => n.Value ).ToList()