Check all checkbox in gridview

if you want to check or uncheck all checkboxes of a particular column in a GridView control in, 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%">
                                    <asp:CheckBox ID="chk_checkall" runat="server" Checked="false" 
                                        onclick="SelectAll(this);" Text="All" />
                                    <asp:CheckBox ID="statusCheck" runat="server" />
                                    <asp:HiddenField ID="checkid" runat="server" Value='<%# Eval("Check") %>' />
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />

Add this code on the load event of the page for binding gridview control:

Dim dt as new DataTable()
grdListUser.DataSource= dt

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)
            var countCheckBoxes = objCheckBoxes.length;
            if (countCheckBoxes == 1) {
                if ("grdListUser") >= 0 &&"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;

Author: Pavan

I am developer have good knowledge of ver 05,08,10 and good hand in sql server.Proficient in Object Oriented Programming and javascript, jQuery. Achievements - Integrate Spotfire, appnexus API with sql server. Hobbies - Blogging ,Games, Movies ,Teaching,Keeping myself update with new technologies