Click here to Skip to main content
16,020,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here Is C# Code..
C#
protected void ChkAll_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox ChkAll = (CheckBox)GridView1.HeaderRow.FindControl("ChkAll");

        if (ChkAll.Checked == true)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("ChkOne");
                chk.Checked = true;

            }
        }
        else
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("ChkOne");
                chk.Checked = false;

                ChkAll.Checked = false;
            }
        }
    }
}

THis is my ASPX Code
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
     CssClass="data" Width="50%" AllowSorting="True" DataSourceID="dsDetails"
        AllowPaging="True" PageSize="5">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="ChkAll" runat="server" AutoPostBack="true" oncheckedchanged="ChkAll_CheckedChanged"/>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="ChkOne" runat="server" AutoPostBack="true"/>
                </ItemTemplate>
            </asp:TemplateField>
Posted
v2
Comments
Sanjeev Alamuri 26-Feb-13 0:44am    
The reason is, there is no event for "ChkOne" CheckBox like "ChkAll". please write "OnCheckedChanged" for "ChkOne" and write the logic in that function.
Exactly...

1 solution

create this event on .cs page
-----------------------------
C#
protected void ChkOne_CheckedChanged(object sender, EventArgs e)
    {
            bool flag = true;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("ChkOne");
                if(!chk.Checked)
                {
                  flag = false;
                  break;
                }
            }
          ((CheckBox)GridView1.HeaderRow.FindControl("ChkAll")).Checked = flag;

    }



replace item template code on .aspx page
----------------------------------------
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
     CssClass="data" Width="50%" AllowSorting="True" DataSourceID="dsDetails"
        AllowPaging="True" PageSize="5">
 <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="ChkAll" runat="server" AutoPostBack="true" oncheckedchanged="ChkAll_CheckedChanged"/>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="ChkOne" runat="server" AutoPostBack="true" oncheckedchanged="ChkOne_CheckedChanged"/>
                </ItemTemplate>
            </asp:TemplateField>
</asp:GridView>
 
Share this answer
 
Comments
Arpit6016 26-Feb-13 10:47am    
Thanx khalid92... Thank You....
With Help of this code i done my work

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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