Monday, July 23, 2012

How to check all Checkbox inside a Gridview in Asp.net



Design View :-


                       <HeaderTemplate>
                            <asp:CheckBox ID="CheckAll" runat="server" AutoPostBack="True"    oncheckedchanged="CheckAll_CheckedChanged"   />
                            <asp:Label ID="LblApproved" runat="server" Text="Approved All" />
                        </HeaderTemplate>



                      <ItemTemplate>
                            <asp:CheckBox ID="ChkApprove" runat="server" Checked='<%# Eval("Approved") %>' />                          
                        </ItemTemplate>



C# Code :-


 protected void CheckAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)GridView1.HeaderRow.FindControl("CheckAll");
            if (chk.Checked)
            {
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    CheckBox chkrow = (CheckBox)GridView1.Rows[i].FindControl("ChkApprove");
                    chkrow.Checked = true;
                }


            }
            else
            {
                for (int i = 0; i < GridViewBankReceipt.Rows.Count; i++)
                {
                    CheckBox chkrow = (CheckBox)GridView1t.Rows[i].FindControl("ChkApprove");
                    chkrow.Checked = false;
                }
            }
        }


No comments:

Post a Comment