how to get selected items count in asp:checkboxlist

前端 未结 4 1547
离开以前
离开以前 2021-02-15 18:01

i have a checkboxlist control

 
 

相关标签:
4条回答
  • 2021-02-15 18:09

    Use this single line of code:

    int selectedCount = chkselectedItems.Items.Cast<ListItem>().Count(li => li.Selected);
    
    0 讨论(0)
  • 2021-02-15 18:23

    Using Linq

     var count=  chkmenuitemdef.Items.Cast<ListItem>().Where(c => c.Selected).Count();
    
    0 讨论(0)
  • 2021-02-15 18:31

    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

    0 讨论(0)
  • 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 ) );
    }
    
    0 讨论(0)
提交回复
热议问题