Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I was using thge following code to count all checked items in my checkbox list control.

C#
var totalcount = CheckBoxList1.Items.Cast<ListItem>().Where(item => item.Selected).Count()


But now i would like to use single checkbox entries instead of a checkbox list. How could this code be changed to the count all checked checkboxes on a page?
Posted

Solution #1 is good, but if you like Linq you can use this:

int total = this.Controls.Cast<Control>().Count(x => x is CheckBox && ((CheckBox)x).Checked);
 
Share this answer
 
v3
yes it will work if it takes under panel
like bellow
aspx
<asp:panel id="Panel1" runat="server" xmlns:asp="#unknown">

<asp:checkbox id="CheckBox1" checked="true" runat="server">
<asp:checkbox id="CheckBox2" runat="server">
<asp:checkbox id="CheckBox3" runat="server">
<asp:checkbox id="CheckBox4" checked="true" runat="server">
<asp:checkbox id="CheckBox5" runat="server">
<asp:checkbox id="CheckBox6" runat="server">
<asp:checkbox id="CheckBox7" runat="server">

aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
int count = 0;
foreach (Control c in Panel1.Controls)
{
if (c is CheckBox)
{
if (((CheckBox)c).Checked)
count = count + 1;
}
}

ClientScript.RegisterClientScriptBlock(GetType(), "sas", "javascript:alert('" + count + "');", true);
 
Share this answer
 
i figured out a work around, as it turns out there is an issue with accessing controls directly inside a ajax control toolkit tab container. My workaround was to put a panel inside of the tab container and then count the controls inside the panel.
 
Share this answer
 
In c#
int count=0;
foreach (Control c in this.form1.Controls)
{
if (c is CheckBox)
{
if (((CheckBox)c).Checked)
count = count + 1;
}
}
ClientScript.RegisterClientScriptBlock(GetType(), "sas", "javascript:alert('"+count+"');", true);

In Jquery U can do like this

var count = $("input[type=checkbox]:checked").length;
alert(count);
 
Share this answer
 
Comments
Dustin Prevatt 25-Feb-13 14:48pm    
I tried this and it always say 0 even if there are checkboxes checked..
Dustin Prevatt 25-Feb-13 15:29pm    
My page has a ajax tab container and the checkboxes are inside it. How could I count controls within that?
Shobhana.n 26-Feb-13 8:33am    
You are trying to take it in C#?
Not in javascrpt or Jquery??
Dustin Prevatt 26-Feb-13 13:20pm    
Yes C#, i figured out a work around, as it turns out there is an issue with accessing controls directly inside a ajax control toolkit tab container. My workaround was to put a panel inside of the tab container and then count the controls inside the panel.
The page class contains a property called Controls. You can iterate through that list, and check the type. If it's a checkbox, cast it as one, access the "Checked" property, and add. Here's some sample code:

C#
int checkedBoxes = 0;
foreach (Control c in Controls)
   if (c is CheckBox)
      if (((CheckBox)c).Checked)
         checkedBoxes++;
 
Share this answer
 
Comments
Richard C Bishop 25-Feb-13 13:49pm    
Nice one!
Sergey Alexandrovich Kryukov 19-Mar-13 2:37am    
1) Code can be improved using "as" operator with check for null, 2) it should generally be recursive
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900