How to get selected item in CheckBoxList in Asp.net

I have a CheckBoxList on my page. Is there a way to get all selected item values ​​using linq?

What is the best way to get selected item values ​​in a CheckBoxList?

+5
source share
3 answers

You can go about this by taking the list of checkbox elements and converting them to ListItems, and from the collection select those that are selected, for example:

var selectedItems = yourCheckboxList.Items.Cast<ListItem>().Where(x => x.Selected);
+19
source

Here is an easy way

foreach (System.Web.UI.WebControls.ListItem oItem in rdioListRoles.Items)
{
    if (oItem.Selected) // if you want only selected
    {
       variable  = oItem.Value;
    }
    // otherwise get for all items
    variable  = oItem.Value;
}
+4
source
List<string> selectedValues = chkBoxList1.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToList();
+2
source

All Articles