How to loop through a checkboxlist and to find what's checked and not checked?
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()
for (int i = 0; i < clbIncludes.Items.Count; i++)
if (clbIncludes.GetItemChecked(i))
// Do selected stuff
else
// Do unselected stuff
If the the check is in indeterminate state, this will still return true. You may want to replace
if (clbIncludes.GetItemChecked(i))
with
if (clbIncludes.GetItemCheckState(i) == CheckState.Checked)
if you want to only include actually checked items.
Try something like this:
foreach (ListItem listItem in clbIncludes.Items)
{
if (listItem.Selected) {
//do some work
}
else {
//do something else
}
}