Check/Uncheck the GridView CheckBox in ASP.NET Using VB.NET
Here we will see how to Check/uncheck all CheckBoxes within a GridView in ASP.NET with the help of VB.NET.
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 check boxes will be unchecked.
Now creating a article table in the SQL server database. The table looks like the below.
Now Create a web application in asp. net.
Drag and drop a GridView control and two Button control on the Form.
.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 " />
<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.
Now Click on the Check Button.
Now click on the uncheck Button to uncheck all the checkbox.