if you want to check or uncheck all checkboxes of a particular column in a GridView control in asp.net, you can do this using JavaScript. this example shows that when you check checkbox on the header then all checkboxes control will be check
In this example we are using TemplateField inside the GridView control and put a CheckBox in the ItemTemplate as well as another CheckBox in the HeaderTemplate of the TemplateField, see this:
<asp:GridView ID="grdListUser" runat="server" Width="43%"> <Columns> <asp:TemplateField> <HeaderTemplate> <asp:CheckBox ID="chk_checkall" runat="server" Checked="false" onclick="SelectAll(this);" Text="All" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="statusCheck" runat="server" /> <asp:HiddenField ID="checkid" runat="server" Value='<%# Eval("Check") %>' /> </ItemTemplate> <ItemStyle HorizontalAlign="Left" /> <HeaderStyle HorizontalAlign="Left" /> </asp:TemplateField> </Columns> </asp:GridView>
Add this code on the load event of the page for binding gridview control:
Dim dt as new DataTable() dt.columns.Add("UserName") dt.Rows.Add() dt.Rows(0).Item(1)="Ankur" dt.Rows.Add() dt.Rows(0).Item(1)="Author" dt.Rows.Add() dt.Rows(0).Item(1)="John" grdListUser.DataSource= dt grdListUser.Databind()
Add this JavaScript in the page’s head section:
<script language="javascript" type="text/javascript"> function SelectAll(obj) { var CheckValue = obj.checked; var objCheckBoxes = document.getElementsByTagName("input"); if (!objCheckBoxes) return; var countCheckBoxes = objCheckBoxes.length; if (countCheckBoxes == 1) { if (objCheckBoxes.name.indexOf("grdListUser") >= 0 && objCheckBoxes.name.indexOf("statusCheck") >= 0) objCheckBoxes.checked = CheckValue; } else { // set the check value for all check boxes for (var i = 0; i < countCheckBoxes; i++) if (objCheckBoxes[i].type == "checkbox") if (objCheckBoxes[i].name.indexOf("grdListUser") >= 0 && objCheckBoxes[i].name.indexOf("statusCheck") >= 0) objCheckBoxes[i].checked = CheckValue; } } </script>