Check multiple items in ASP.NET CheckboxList

I am trying to check multiple values ​​in ASP.NET CheckboxList, but I could not.
I wrote:

chkApplications.SelectedValue = 2; chkApplications.SelectedValue = 6; 

But he just selects an element with a value of "6"
What's wrong?

+8
c # checkboxlist
source share
4 answers

The best technique that will work for you is as follows:

 chkApplications.Items.FindByValue("2").Selected = true; chkApplications.Items.FindByValue("6").Selected = true; 

OR you can just do it like ...

  foreach (ListItem item in chkApplications.Items) { if (item.Value == "2" || item.Value == "6") { item.Selected = true; } } 
+18
source share
 foreach (var item in cb.Items.Cast<ListItem>() .Where (li => li.Value == "2" || li.Value == "6")) item.Selected = true; 
+5
source share

you can put the value in a list ( MyList ) and use FindByValue to check them.

 foreach (var item in MyList) { checkBoxList.Items.FindByValue(item.id).Selected = true; } 
+5
source share

Instead of trying to select an item through chkApplications.SelectedValue , try chkApplications.Items.Item(2).Selected = True chkApplications.Items.Item(6).Selected = True

-one
source share

All Articles