DooDoo
DooDoo

Reputation: 13437

how to get selected item in CheckBoxList in Asp.net

I have a CheckBoxList in my page.is there any way to get all selected item values using linq?

what is the best way to get selected item values in CheckBoxList?

Upvotes: 6

Views: 30440

Answers (3)

Laird Streak
Laird Streak

Reputation: 399

Here's 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;
}

Upvotes: 4

Arif Ansari
Arif Ansari

Reputation: 480

List<string> selectedValues = chkBoxList1.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToList();

Upvotes: 2

A. Tapper
A. Tapper

Reputation: 1271

You could go about this by taking the items of the checkbox list and converting them to ListItems and from that collection fetch those who is selected, like this:

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

Upvotes: 19

Related Questions