C# and jQuery code to update all checkboxes for multiple ASP.NET grids on a page

I dad a need to update all checkboxes on client side in a grid. Found a great article posted here by Cinnie Patel. I just added a slight mod to make it function on multiple grids on a page. In a nutshell the jQuery code is something like:

function SelectAll(CheckBoxControl,GridName)
{
   var newState;
   if (CheckBoxControl.checked == true)
   {
      newState = true;
   }
   else
   {
      newState = false;
   }
   var i;
   for (i=0; i < document.forms[0].elements.length; i++) 
   { 
      if ((document.forms[0].elements[i].type == 'checkbox') 
           && (document.forms[0].elements[i].name.indexOf(GridName) > -1))
      {
         document.forms[0].elements[i].checked = newState;
      }
   }
}

and then you place the code to call this on the header item of a grid (assuming you are using checkboxes in the header as well to select all/select none) like this:

<input id="SelectAllCheckboxes" onclick="SelectAll(this,'##NAME OF YOUR GRID GOES HERE##')" type="checkbox" />

…….
Hope this helps someone and kudos to original poster.

Print Friendly, PDF & Email