Check/Uncheck All Checkboxes in Asp.Net Gridview

Introduction:

In this article we will see how to check and uncheck all CheckBoxes present in an ASP.Net GridView control.

Background:

While programming many times we need to use a CheckBox control in an ASP.Net GridView for allowing selection of a particular row of a GridView. In some cases a GridView can contain 100 of records and if the user wants to select all rows of the GridView then selecting each row one by one is very tedious and a time-consuming job. To make it easier we will see how to write a simple method for selecting all CheckBoxes in a GridView. Take a look at the following snippets.
  1. Create a new website. On the default.aspx page keep the GridView control with AutogeneratedColumn = false. The source for the GridView should look like below.

    asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
            <Columns>
            <asp:TemplateField ShowHeader="false">
           <ItemTemplate>
           <asp:CheckBox ID="chkid" runat="server" />
            </ItemTemplate>
           </asp:TemplateField>
            <asp:BoundField DataField="Auth_Name" HeaderText="Author Name" />
            <asp:BoundField DataField="Auth_Loc" HeaderText="Location" />
            </Columns>
            </asp:GridView>
  2. Keep two buttons on the form; one for checking all checkboxes and the other for unchecking.
  3. The CheckState method is for performing a check/uncheck operation in the GridView. This method will take true/false as input to check/uncheck the checkboxes of the GridView.

    private void CheckState(bool p)
        {

            foreach (GridViewRow row in GridView1.Rows)
           {
               CheckBox chkcheck = (CheckBox)row.FindControl("chkid");
                     chkcheck.Checked = p;
            }
        }

  4. Bind your GridView and run.

Conclusion:

By using a simple method we are able to check/uncheck 100 of rows also in GridView.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值