i have a checkboxlist control
我有一个checkboxlist控件
<asp:CheckBoxList ID="chkselectedItems" Font-Size="12px" runat="server">
</asp:CheckBoxList>
and i created listitems on it dynamically.If i checked more than one item from the checkbox list, How i get the selected item count using asp.net
我动态创建了列表项。如果我从复选框列表中检查了多个项目,我如何使用asp.net获取所选项目数量
thanks
谢谢
4 个解决方案
#1
6
Edit
编辑
int numSelected = 0;
foreach (ListItem li in chkselectedItems.Items)
{
if (li.Selected)
{
numSelected = numSelected + 1;
}
}
Response.Write("Total Number Of CheckBoxes Selected:");
Response.Write(numSelected);
pre
预
public string[] CheckboxListSelections(System.Web.UI.WebControls.CheckBoxList list)
{
ArrayList values = new ArrayList();
for(int counter = 0; counter < list.Items.Count; counter++)
{
if(list.Items[counter].Selected)
{
values.Add(list.Items[counter].Value);
}
}
return (String[]) values.ToArray( typeof( string ) );
}
#2
30
Use this single line of code:
使用这一行代码:
int selectedCount = chkselectedItems.Items.Cast<ListItem>().Count(li => li.Selected);
#3
3
We will iterate through the checkboxlist and use a counter variable for counting selected items. For every item in checkboxlist we will add 1 to counter variable selCount if item is checked
我们将遍历复选框列表并使用计数器变量来计算所选项目。对于checkboxlist中的每个项目,如果选中了item,我们将向计数器变量selCount添加1
int selCount = 0;
for(int i= 0; i< chklst.Items.Count; i++)
if(chklst.Items[i].Selected)
selCount++;
// Now selCount will contain the count of selected items
//现在selCount将包含所选项目的计数
#4
0
Using Linq
使用Linq
var count= chkmenuitemdef.Items.Cast<ListItem>().Where(c => c.Selected).Count();
#1
6
Edit
编辑
int numSelected = 0;
foreach (ListItem li in chkselectedItems.Items)
{
if (li.Selected)
{
numSelected = numSelected + 1;
}
}
Response.Write("Total Number Of CheckBoxes Selected:");
Response.Write(numSelected);
pre
预
public string[] CheckboxListSelections(System.Web.UI.WebControls.CheckBoxList list)
{
ArrayList values = new ArrayList();
for(int counter = 0; counter < list.Items.Count; counter++)
{
if(list.Items[counter].Selected)
{
values.Add(list.Items[counter].Value);
}
}
return (String[]) values.ToArray( typeof( string ) );
}
#2
30
Use this single line of code:
使用这一行代码:
int selectedCount = chkselectedItems.Items.Cast<ListItem>().Count(li => li.Selected);
#3
3
We will iterate through the checkboxlist and use a counter variable for counting selected items. For every item in checkboxlist we will add 1 to counter variable selCount if item is checked
我们将遍历复选框列表并使用计数器变量来计算所选项目。对于checkboxlist中的每个项目,如果选中了item,我们将向计数器变量selCount添加1
int selCount = 0;
for(int i= 0; i< chklst.Items.Count; i++)
if(chklst.Items[i].Selected)
selCount++;
// Now selCount will contain the count of selected items
//现在selCount将包含所选项目的计数
#4
0
Using Linq
使用Linq
var count= chkmenuitemdef.Items.Cast<ListItem>().Where(c => c.Selected).Count();