Reputation: 13437
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
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
Reputation: 480
List<string> selectedValues = chkBoxList1.Items.Cast<ListItem>().Where(li => li.Selected).Select(li => li.Value).ToList();
Upvotes: 2
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