Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Below code doesn't work for select all checkbox. after clicking on select all checkbox,all checkboxes remain unchecked.

What I have tried:

ASP.NET
<asp:GridView ID="grdMember" runat="server" CellPadding="4" ForeColor="#333333" 
                GridLines="None" HorizontalAlign="Center">
                <columns>
                <asp:TemplateField HeaderText="Select All">
                <headertemplate>
                <asp:CheckBox ID="checkbox2" OnCheckedChanged="CheckAll" runat="server" />
                
                <itemtemplate>
                <asp:CheckBox ID="IDCheckbox" runat ="server" />


C#
protected void CheckAll(object sender, EventArgs e)
    {
        CheckBox chckheader = (CheckBox)grdMember.HeaderRow.FindControl("checkbox2");
        foreach (GridViewRow row in grdMember.Rows)
        {
            CheckBox chckrw = (CheckBox)row.FindControl("IDCheckbox");
            if (chckheader.Checked == true)
            {
                chckrw.Checked = true;

            }
            else
            {
                chckrw.Checked = false;
            }

        }
    }
Posted
Updated 28-Sep-18 5:22am
v3
Comments
Maciej Los 26-Sep-18 2:07am    
And...
What's wrong with your code?
Vincent Maverick Durano 28-Sep-18 11:24am    
The problem is that AutoPostback isn't set for the CheckBox, thus OnCheckChanged event never fires.

1 solution

Quote:
<asp:CheckBox ID="checkbox2" OnCheckedChanged="CheckAll" runat="server" />


Your CheckAll method will never get fired because you haven't set AutoPostback to True for your CheckBox. Set AutoPostback to true so the OnCheckedChanged event will fire.

ASP.NET
<asp:CheckBox ID="checkbox2" OnCheckedChanged="CheckAll" runat="server" AutoPostBack="True" />
 
Share this answer
 
Comments
Maciej Los 28-Sep-18 11:30am    
5ed!
Vincent Maverick Durano 28-Sep-18 11:33am    
Thank you! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900