Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何知道复选框列表项(从动态)是否已选中?_C#_Asp.net - Fatal编程技术网

C# 如何知道复选框列表项(从动态)是否已选中?

C# 如何知道复选框列表项(从动态)是否已选中?,c#,asp.net,C#,Asp.net,我在asp.net c中创建了一个2个复选框列表,该复选框列表的列表项是从数据库动态填充的,那么我想检查这些复选框列表是否被选中?请帮助我…尝试以下代码: String values = ""; for (int i=0; i< cbl.Items.Count; i++) { if(cbl.Items[i].Selected) { values += cbl.Items[i].V

我在asp.net c中创建了一个2个复选框列表,该复选框列表的列表项是从数据库动态填充的,那么我想检查这些复选框列表是否被选中?请帮助我…

尝试以下代码:

    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
}

或者您可以使用此代码

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

您可以将此答案附加到先前的答案中,以便读者在一个答案中有两个备选答案。。。。。。