Question

i am having foreach loop on ChekboxList like

foreach (ListItem li in chkUnitCategory.Items)
{
}

now i need to perform a task in which when checkbox of index 0 is selected all the other checkbox in list must be selected and vice versa.

so how can i perform this task using index of check box list.

Was it helpful?

Solution 2

Just a note changing collection is not allowed when you iterating over it.

If you need, try:

foreach (ListItem li in chkUnitCategory.Items)
{
   li.Selected = chkUnitCategory.Items[0].Selected;
}

OTHER TIPS

for (int i = 0; i < chkUnitCategory.Items.Count; i++)
{
  chkUnitCategory.Items[i].Selected = chkUnitCategory.Items[0].Selected;
}

This is how you would write it utilizing LINQ:

chkUnitCategory.Items.ForEach(item => item.Selected = chkUnitCategory.Items.First().Selected)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top