Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get selected item in CheckBoxList in Asp.net

Tags:

c#

asp.net

linq

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?

like image 892
Arian Avatar asked Jan 11 '12 08:01

Arian


3 Answers

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);
like image 150
A. Tapper Avatar answered Sep 30 '22 09:09

A. Tapper


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;
}
like image 21
Laird Streak Avatar answered Sep 30 '22 10:09

Laird Streak


List<string> selectedValues = chkBoxList1.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToList();
like image 36
Arif Ansari Avatar answered Sep 30 '22 09:09

Arif Ansari