简体   繁体   English

检查 ASP.NET CheckboxList 中的多项

[英]Check multiple items in ASP.NET CheckboxList

I try to check multiple values in ASP.NET CheckboxList but I couldn't.我尝试检查 ASP.NET CheckboxList 中的多个值,但我做不到。
I Wrote:我写:

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

But it just selects item with value '6'但它只是选择值为“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;
        }
    }

you can put the value in a list ( MyList ), and use FindByValue to check them.您可以将值放在一个列表 ( MyList ) 中,并使用FindByValue来检查它们。

foreach (var item in MyList)
{
    checkBoxList.Items.FindByValue(item.id).Selected = true;
}
foreach (var item in cb.Items.Cast<ListItem>()
        .Where (li => li.Value == "2" || li.Value == "6"))
   item.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而不是通过chkApplications.SelectedValue尝试 select 项目尝试chkApplications.Items.Item(2).Selected = True chkApplications.Items.Item(6).Selected = True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM