1

I am trynig to select all checkbox when header checkbox is selected.

I write proper code for that but do'nt know why it's not working.

Can anyone check the issue.

My code--

    <head runat="server">
    <title></title>
    <script type="text/javascript">
        function SelectAllCheckboxes1(chk) {
            $('#<%=GridView1.ClientID%>').find("input:checkbox").each(function () {
                if (this != chk) { this.checked = chk.checked; }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:gridview ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="cbSelectAll" runat="server" onclick="javascript:SelectAllCheckboxes1(this);" AutoPostBack="true"/>    
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chk" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            </Columns>   
        </asp:gridview>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:HRMSConnectionString %>" 
            SelectCommand="SELECT [Name] FROM [Languages]"></asp:SqlDataSource>
    </div>
    </form>
</body>
2
  • Do u want to do it with javascript ?? .... Commented May 3, 2014 at 12:09
  • No particular language.... Commented May 3, 2014 at 12:12

1 Answer 1

2

Try this code-

default.aspx.cs

protected void sellectAll(object sender, EventArgs e)
{
    CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("chkb1");
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkb2");
        if (ChkBoxHeader.Checked == true)
        {
            ChkBoxRows.Checked = true;
        }
        else
        {
            ChkBoxRows.Checked = false;
        }
    }
}

Change grid code-

 <asp:gridview ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="chkb1" runat="server"  AutoPostBack="true" OnCheckedChanged="sellectAll"/>    
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chkb2" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        </Columns>   
    </asp:gridview>
Sign up to request clarification or add additional context in comments.

1 Comment

Your code will work, but I think OP was trying to get this done though javascript or jquery?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.