C# 如何知道复选框列表项(从动态)是否已选中?
我在asp.net c中创建了一个2个复选框列表,该复选框列表的列表项是从数据库动态填充的,那么我想检查这些复选框列表是否被选中?请帮助我…尝试以下代码: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
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));
您可以将此答案附加到先前的答案中,以便读者在一个答案中有两个备选答案。。。。。。