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);
}
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
}
}
I think the best way to do this is to use CheckedItems
:
foreach (DataRowView objDataRowView in CheckBoxList.CheckedItems)
{
// use objDataRowView as you wish
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With