How To: Use a CheckBox In A Template Column And Still Access GridViewRow Data

Teemu "Joteke" Keiski has helped me solve a very trivial task. I had the simple request of accessing a GridView row data by checking a checkbox within that row. Based on the row data, I'd perform a particular task.

My GridView had a few bound columns with a few template columns. One of the template columns contained a checkbox. When the checkbox was clicked, I wanted to run the Checked event and pull information from the row that it was clicked. So, using the Checked event, I can then take that information and based on that information, I can perform a task. The example I have provided below does just that. It is a simple example where the first field in the GridView is a username from the Membership.GetUsers collection. The second and third fields are two roles that the user may or may not be part of. I want the administrator to click the checkbox to add or remove the user from a particular role. Here's the example:

     Sub ToggleUserAndRole(ByVal s as Object, ByVal e as EventArgs)
          'Convert Object to CheckBox
          Dim MyCheckBox as CheckBox = CType(s, CheckBox)

                'Convert NamingContainer to GridViewRow
          Dim MyRow as GridViewRow = CType(MyCheckBox.NamingContainer, GridViewRow)

                'Obtain Current Cell and CellIndex
          Dim CurrentCell as TableCell = CType(MyCheckBox.Parent, TableCell)
          Dim CellIndex as Integer = MyRow.Cells.GetCellIndex(CurrentCell)

                'Obtain Username from first column
          Dim Username as String = MyRow.Cells(0).Text

                'Obtain "Role" Checked
          Dim Role as String
          If CellIndex = 1 then
               Role = "Administrator"
          Else
               Role = "Member"
          End If

                'Add or Remove User to Role
          If MyCheckBox.Checked then
               Roles.AddUserToRole(Username, Role)
          Else
               Roles.RemoveUserFromRole(Username, Role)
          End If
End Sub
Published Wednesday, July 12, 2006 5:05 PM by Jason N. Gaylord

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)