ARTICLE

Check/uncheck all CheckBoxes within a GridView control in VB.NET

Posted by Rohatash Kumar Articles | ASP.NET using VB.NET August 12, 2011
Here we will see how to Check/uncheck all CheckBoxes within a GridView in ASP.NET.
 
Reader Level:

Here we will see how to Check/uncheck all CheckBoxes within a GridView in ASP.NET. To do that we create a asp.net form with GridView control and two Button control on the form. When we click Button checked all the Checkbox will be checked (Mark as tick) and click on the second Button Unchecked all the checkboxes will be unchecked.

Now creating a article table in the SQL server database. The table looks like the below.

figure1.gif

Figure1

Now Create a web application in asp. net.

Drag and drop a GridView control and two Button control on the Form.

figure2.gif

Figure2.

.aspx code

<div>

   <asp:GridView ID="GridView2" runat="server">

        <Columns>

        <asp:TemplateField ShowHeader="false">

        <ItemTemplate>

        <asp:CheckBox ID="chkid" runat="server" />

        </ItemTemplate>

        </asp:TemplateField>

        </Columns>

        </asp:GridView>

        <br />

      <asp:Button ID="Button1" runat="server" onclick="Button1_Click"

            Text="Check " />

&nbsp;&nbsp;&nbsp;

        <asp:Button ID="Button2" runat="server" Text="Uncheck "

            onclick="Button2_Click" />

    </div>

 

.CS code

Imports System.Data.SqlClient

Public Class WebForm1

    Inherits System.Web.UI.Page

 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            Dim con As New SqlConnection("Data Source=(local); uid=sa; pwd=Password$2; database=master")

            con.Open()

            Dim dt As New SqlDataAdapter("select * from Article", con)

            Dim ds As New DataSet()

            dt.Fill(ds, "article")

            GridView2.DataSource = ds

            GridView2.DataBind()

        End If

 

    End Sub

 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        CheckState(True)

    End Sub

 

    Private Sub CheckState(ByVal p As Boolean)

 

        For Each row As GridViewRow In GridView2.Rows

            Dim chkcheck As CheckBox = DirectCast(row.FindControl("chkid"), CheckBox)

            chkcheck.Checked = p

        Next

    End Sub

 

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click

        CheckState(False)

    End Sub

End Class

 

Now run the application and test it.


figure3.gif

 

Figure3

 

Now Click on the Check Button.


figure4.gif

 

Figure4

 

Now click on the uncheck Button to uncheck all the checkbox.


figure3.gif

 

Figure5

Login to add your contents and source code to this article
share this article :
post comment
 

good article. thanks

Posted by Andrew Aug 16, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor