Handling Checkbox and Button Postbacks in a Gridview with Visual Basic 2008

You can simply copy and paste the bits of code you need into your project or text editor.

 


Public Partial Class GridviewExample
    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
            GridView1.DataSource = dtTestData()
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim checkbox As CheckBox = DirectCast(sender, CheckBox)
        Dim row As GridViewRow = DirectCast(checkbox.NamingContainer, GridViewRow)
        Dim UserId As Integer = CInt(GridView1.DataKeys(row.DataItemIndex).Value)
        Dim UserInfo As String = ""
        Dim Status As Boolean = checkbox.Checked
        Dim strStatus As String = ""
        UserInfo = " ("
        UserInfo += GridView1.Rows(row.DataItemIndex).Cells(1).Text + " "
        UserInfo += GridView1.Rows(row.DataItemIndex).Cells(2).Text
        UserInfo += ")"
        If Status Then
            strStatus = "Approved"
        Else
            strStatus = "NOT Approved"
        End If
        lblStatus.Text += "CheckBox1_CheckedChanged Fired for User Id = " + UserId.ToString + UserInfo + " and new status is " + strStatus + "<br /><br />"
    End Sub
    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        '
        Dim cmdIndex As Integer = 0
        Dim UserId As Integer = 0
        Dim UserInfo As String = ""
        Dim row As GridViewRow
        Dim chkApproved As CheckBox
        '
        cmdIndex = Int32.Parse(e.CommandArgument)
        UserId = GridView1.DataKeys(cmdIndex).Values("UserId")
        row = GridView1.Rows(cmdIndex)
        chkApproved = CType(row.Cells(3).FindControl("CheckBox1"), CheckBox)
        UserInfo = " ("
        UserInfo += GridView1.Rows(cmdIndex).Cells(1).Text + " "
        UserInfo += row.Cells(2).Text
        If chkApproved.Checked Then
            UserInfo += " Approved"
        Else
            UserInfo += " NOT Approved"
        End If
        UserInfo += ")"
        lblStatus.Text += "GridView1_RowCommand Fired for User Id = " + UserId.ToString + UserInfo + " and button command name = " + e.CommandName + "<br /><br />"

    End Sub

    Public Function dtTestData() As DataTable
        Dim drRow As DataRow
        Dim dtData As New DataTable
        Dim strApproved As String = "Y"
        Dim DateOfBirth As String = ""
        Dim DateRequested As String = ""
        '
        '   Define DataTable Columns
        '
        dtData.Columns.Add(New DataColumn("UserId", GetType(Integer)))
        dtData.Columns.Add(New DataColumn("FirstName", GetType(String)))
        dtData.Columns.Add(New DataColumn("LastName", GetType(String)))
        dtData.Columns.Add(New DataColumn("Approved", GetType(Boolean)))
        dtData.Columns.Add(New DataColumn("Delete", GetType(String)))
        '
        '   Add Test Data.
        '
        drRow = dtData.NewRow
        drRow("UserId") = 1
        drRow("FirstName") = "John"
        drRow("LastName") = "Smith"
        drRow("Approved") = "False"
        drRow("Delete") = "Delete"
        dtData.Rows.Add(drRow)
        drRow = dtData.NewRow
        drRow("UserId") = 2
        drRow("FirstName") = "Alan"
        drRow("LastName") = "Jones"
        drRow("Approved") = "False"
        drRow("Delete") = "Delete"
        dtData.Rows.Add(drRow)
        drRow = dtData.NewRow
        drRow("UserId") = 3
        drRow("FirstName") = "David"
        drRow("LastName") = "Wilson"
        drRow("Approved") = "False"
        drRow("Delete") = "Delete"
        dtData.Rows.Add(drRow)
        Return dtData
    End Function

End Class
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