Check multiple items in ASP.NET CheckboxList

I try to check multiple values in ASP.NET CheckboxList but I couldn’t.

I Wrote :

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

But it just selects item with value ‘6’
What’s wrong ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The best technique that will work for you is the following:

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

OR you can simply do it like…

  foreach (ListItem item in chkApplications.Items)
    {
        if (item.Value == "2" || item.Value == "6")
        {
            item.Selected = true;
        }
    }

Method 2

foreach (var item in cb.Items.Cast<ListItem>()
        .Where (li => li.Value == "2" || li.Value == "6"))
   item.Selected = true;

Method 3

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;
}

Method 4

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x