how to get selected items count in asp:checkboxlist

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

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();


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!