I have a GridView- name (default GridView1). This GridView contains a Checkbox named (chkSelect).
I have 2 link buttons- CheckAll (id- lnkCheckAll), UnCheckAll(id- lnkUnCheckAll).
By clicking the CheckAll link button, all the checkboxes in the gridView1 will be checked and similarly by clicking the UncheckAll link button, all the checkboxes in the GridView will get unchecked.
If the parameter is "true", then all the checkboxes inside the GridView1 will be checked and if is "false", then all the checkboxes in the GridView1 will be unChecked.
var grid = document.getElementById("<%= GridView1.ClientID %>");
// declare variable to contain the grid cell
if (grid.rows.length > 0)
for (i = 1; i < grid.rows.length; i++)
cell = grid.rows[i].cells;
if (cell.childNodes.type == "checkbox")
cell.childNodes.checked = b;
So i prefer to attach the "click" event in the RowDataBound event of the GridView as shown below:
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
"true" + "')");
"false" + "')");
That's all. Cheers..
No need to do anything else.
In the RowDataBound event of the GridView, it is checking that if the Row id DataRow (means except Header and Footer), then it attach the "onClick" event for both the link buttons.
For the lnkSelectAll, it's passing the value as "true". It means it will check all the checkboxes and for the lnkUnSelectAll link button, it is passing the value as "false" so it will uncheck all the checkboxes which are inside the GridView.
Hope it will help for the coding prospective.
Also good real time code snippet which will help to other to implement the similar functionality.
In design source link button you are using CheckAll (id- lnkCheckAll), UnCheckAll(id- lnkUnCheckAll). But In server side, code behind you are using lnkSelectAll and lnkUnSelectAll. Very small correction this is.