Consider a GridView with a checkbox column. We want to select all checkboxes or clear all checkboxes within this column using a Select All checkbox in the header row.

GridView Definition

The GridView has a template column. Template defines the header row as well as the item row. In the header row, we have the Select All checkbox. And within in the item row, we have a checkbox to select an item.

The GridView will render as below:

SelectAllGridView

JQuery for checkbox events

We will use jQuery for Select All function. Check Select All checkbox: Checks all checkboxes in the column. Clear Select All checkbox: Clears all checkboxes in the column. The reverse should also work. Check all checkboxes in the column: Checks Select All checkbox. Clear all checkboxes in the column: Clears Select All checkbox.

The selector for Select All checkbox is #chkSelectAll. For individual checkboxes in the checkbox column, the selector is input:checkbox[name$=chkSelect]. The previous selector picks all checkboxes with name ending with chkSelect. To check a checkbox, add the checked attribute. To clear a checkbox, remove the checked attribute.

With jQuery, client side event handling is simple for ASP.NET controls.

Selecting all checkboxes in GridView column using jQuery
Tagged on: