Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 323
Shahin Avatar asked Jul 10 '11 10:07

Shahin


People also ask

How can we store multiple checkbox values in database using ASP NET?

Place your checkboxes in side a panel. Then looping through the controls of type checkbox get the chcked checkbox value as comma separated string. After getting the value as comma separated you can insert it in database.


1 Answers

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;
        }
    }
like image 155
Muhammad Akhtar Avatar answered Oct 02 '22 18:10

Muhammad Akhtar