Connecting Tech Pros Worldwide Help | Site Map

Check All CheckBoxes In GridView Via JavaScript Problem

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#1: May 6 '08
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
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#2: May 7 '08

re: Check All CheckBoxes In GridView Via JavaScript Problem


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
Reply