开发者

Check multiple items in ASP.NET CheckboxList

开发者 https://www.devze.com 2023-03-19 08:41 出处:网络
I try to check multiple values in ASP.NET CheckboxList bu开发者_运维百科t I couldn\'t. I Wrote :

I try to check multiple values in ASP.NET CheckboxList bu开发者_运维百科t I couldn't.

I Wrote :

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

But it just selects item with value '6'

What's wrong ?


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


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消