By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,156 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,156 IT Pros & Developers. It's quick & easy.

Check All CheckBoxes In GridView Via JavaScript Problem

Frinavale
Expert Mod 5K+
P: 9,731
I'm working on an ASP.NET application using VB.NET for server side code.

I have a GridView listing a bunch of stuff. Above the GridView I have a checkbox named "ChkBx_SelectAll". If this checkbox is checked, all of the rows in the GridView are checked, if this checkbox is unchecked, all of the rows in the GridView are unchecked.

This checking/unchecking of the checkboxes in the GridView is handled by a JavaScript method:

Expand|Select|Wrap|Line Numbers
  1. function checkAllCheckboxes(idOfControllingCheckbox,gridViewClientID)
  2. {
  3.     var grid = document.getElementById(gridViewClientID);
  4.     var cell;
  5.  
  6.     if (grid.rows.length > 0)
  7.     {
  8.         for (i=0; i<grid.rows.length; i++)
  9.         {
  10.             cell = grid.rows[i].cells[0];
  11.             for (j=0; j<cell.childNodes.length; j++)
  12.             {           
  13.                 if (cell.childNodes[j].type =="checkbox")
  14.                 {
  15.                    cell.childNodes[j].checked = document.getElementById(idOfControllingCheckbox).checked;
  16.                 }
  17.             }
  18.         }
  19.     }
  20. }
This method is passed the ClientID of the "ChkBx_SelectAll" checkbox, and sets all of the checkboxes in the GridView (whose ClientID is passed into this method as well) to match the "ChkBx_SelectAll" checkbox.

All is well and happy!

Except..............after Unchecking all of the CheckBoxes in my GridView (via the JavaScript method) and then I click a any checkbox(es) in the GridView, that CheckBox is not marked as checked when I'm determining which GridView checkboxes are checked on the server.

If I remove this code and manually uncheck everything by hand, then check any checkbox(es) everything is fine.

Does anyone know why this JavaScript method is forcing my GridView to act in this way?

-Frinny
May 6 '08 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
I solved this problem earlier today.

The problem was not with the above code, this code works fine for selecting all checkboxes in a GridView. My problem was that originally this was done server side using Ajax calls, I forgot to remove the CheckChanged event for the Select All checkbox...so this was getting fired every time my Select All checkbox was changing, which effectively removed or set all the checkboxes in my GridView before I could loop through to discover which were checked.

Lesson of the day:

"Do Not keep any redundant code"

-Frinny
May 7 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.