Click here to Skip to main content
12,451,455 members (54,320 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# checkbox
I was using thge following code to count all checked items in my checkbox list control.

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 25-Feb-13 7:26am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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);
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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);
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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);
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:

int checkedBoxes = 0;
foreach (Control c in Controls)
   if (c is CheckBox)
      if (((CheckBox)c).Checked)
         checkedBoxes++;
  Permalink  
Comments
richcb 25-Feb-13 13:49pm
   
Nice one!
   
1) Code can be improved using "as" operator with check for null, 2) it should generally be recursive
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 26 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100