繁体   English   中英

如何知道(动态地)选中了复选框列表项?

[英]How to know the checkboxlist items (from dynamically) are checked or not?

我在asp.net C#中创建了2个chekboxlist,此chek box列表的列表项是从数据库(动态地)填充的,然后我想检查这些复选框是否被选中? 请帮我...

或者您可以使用此代码

IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>() 
                               where item.Selected 
                               select int.Parse(item.Value));

试试这个代码:

    String values = "";
    for (int i=0; i< cbl.Items.Count; i++)
    {
            if(cbl.Items[i].Selected)
            {
                    values += cbl.Items[i].Value + ",";
            }
    }

    values = values.TrimEnd(',');

或者您可以使用此代码(Linq)

    IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>() 
                               where item.Selected 
                               select int.Parse(item.Value));

尝试这个

string ids=string.Empty;

foreach (ListItem item in checkboxlist1.Items)
        { 
            if(item.Selected)
ids+=item.Value+",";
        }

ids=ids.Trim(',');

您可以使用SelectedIndex来检查清单是否被选中:

if( ckl.SelectedIndex != -1 )
{
// Do Something
}

暂无
暂无

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

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