I’ve been working with the CheckBoxList control and it’s annoying there is no property to get the selected items.
I wrote a simple extension method for ListControl, the abstract class that CheckBoxList and ListBox derive from, to get the selected items:
public static ListItemCollection GetSelectedItems(this ListControl source)
{
ListItemCollection selectedItems = new ListItemCollection();
foreach (ListItem item in source.Items)
{
if (item.Selected)
{
selectedItems.Add(item);
}
}
return selectedItems;
}
I could loop through the selected items like so:
foreach (ListItem item in myCheckBoxList.GetSelectedItems())
{
//Do something with the item
}
It would be even better if Microsoft implemented extension properties so that I can loose the parenthesis.


