[RESOLVED] How to determine if all checkboxes are checked in code behind

I have a page that im using javascript to do a select all / unselect all for my checkboxes on the page.
Now i need to determine in the code behind if all of them are checked, im wondering if it would be easier to just set a session or something when
the select all is checked, but that wouldnt be accurate, because after you select all, you still can uncheck some..

So i need it to check at the time the button is clicked.. there are currectly 30 checkboxes, NOT a checkboxlist.

I tried using the site search but getting an error has occured when i click to search...

Can anyone help?

Assuming you're using <asp:CheckBox> controls, and that you aren't doing this inside something like a DataGrid, the following VB should work.

Dim MyCheckBoxIsChecked as Boolean = MyCheckBoxId.Checked

Of course, you would have to do that for each checkbox.

Or you could create a gigantic If claus that AND'ed them all together:

If MyCheckBoxId01.Checked And MyCheckBoxId02.Checked And MyCheckBoxId03.Checked And MyCheckBoxId04.Checked ... Then

i was hoping for something with less code.. the javascript i have is 3 lines and handles all the checkboxes.. could there be something to convert the javascript i have to C#?


the javascript i have is 3 lines and handles all the checkboxes
[Hmm, that couldn't be jQuery, could it?]

JavaScript and C# function in different environments and work with different object hierarchies.  AFAIK, there is no translation.

In my experience, my time is better spend using copy/paste to generate 30 lines of dumb, repetitive code than it would be debugging a clever routine that attempts to scan all the controls on a page and determine programmatically whether or not it's one that I'm looking for.  (YMMV)

I dont mind doing 30 lines.. figured i'd check if there was something better / different to do the same thing in C#

went ahead and did it with all 30 checkboxes.



you just need a function to walk the control hierarchy which you can pass the test to.

public void ControlWalker(Control ctl, Action<Control> action)  {
        foreach (Control child in ctl.Controls)
            ControlWalker(child, action);

bool allChecked = true;
ControlWalker(Page, ctl => {
       if (ctl is ICheckBoxControl)
              allChecked = allChecked & ((ICheckBoxControl)ctl).Checked;

//or if you don't want to predefine a function:

bool allChecked = true;
Action<Control> walk = null; //hack to allow recursion
walk = (ctl => {
      if (ctl is ICheckBoxControl)
            allChecked = allChecked & ((ICheckBoxControl)ctl).Checked;
      foreach (Control child in ctl.Controls)


thoughts on "[RESOLVED] How to determine if all checkboxes are checked in code behind"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>