可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
i have a checkboxlist control
<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
thanks
回答1:
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:
Use this single line of code:
int selectedCount = chkselectedItems.Items.Cast<ListItem>().Count(li => li.Selected);
回答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
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
回答4:
Using Linq
var count= chkmenuitemdef.Items.Cast<ListItem>().Where(c => c.Selected).Count();