foreach(Listitem item in CheckboxList1.Items)
{
if(item.Selected == true)
{
return true;
}
}
return false;
Is there a better way to check if all checkbox selected is false?
You could use LINQ very easily:
return CheckboxList1.Items
.Cast<ListItem>()
.Any(item => item.Selected);
(The call to Cast
is required because ListItemCollection
doesn't implement IEnumerable<T>
, only the nongeneric collection interfaces.)
If you want to check if ALL checkboxes are checked/unchecked you should use:
return CheckboxList1.Items
.Cast<ListItem>()
.All(item => item.Selected == False);//or True
Or Am I wrong ?
According to MSDN The overload of Any(Of TSource)(IEnumerable(Of TSource), Func(Of TSource, Boolean))
will return true on any item fullfilling the predicate.