ASP.NET CHECKBOX

Check/Uncheck All Checkboxes in Asp.Net Gridview

DOWNLOAD SOURCE HERE

Introduction:

In this article we will see how to check and uncheck all checkboxes presents in gridview control of asp.net.

Background:

While programming so many times we need to take a checkbox control in Asp.net gridview for allowing selection of particular row of gridview. In some cases our gridview contain 100 of records and if user wants to select all rows of gridview selecting row one by one is very tedious and time taking job for making easier this job we will see how to write simple method for selecting all checkboxes in gridview. Take a look at following snippets.

1)      Create one new website. On default.aspx page keep the gridview control with AutogeneratedColumn =false. Source for gridview look like bellow.

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 form one for checking all checkboxes and second for unchecking.

3)      The CheckState method for performing check/uncheck operation in gridview. This method will take true/false as input to check/uncheck the checkboxes of 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 simple method we are able to check/uncheck 100 of rows also in gridview.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s