i have a checkboxlist control
Use this single line of code:
int selectedCount = chkselectedItems.Items.Cast<ListItem>().Count(li => li.Selected);
Using Linq
var count= chkmenuitemdef.Items.Cast<ListItem>().Where(c => c.Selected).Count();
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
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 ) );
}