Trap the CheckedChanged event of checkbox control in ASP.NET datagrid.

Introduction:
This article will show how to trap the CheckedChanged event of checkbox control in ASP.NET datagrid.

Scope:
The example in this article has been tested with VS.Net 2003 and VS.Net 2005 with IE Version 6.0 running on XP and W2K clients. VS.Net 2005 introduced the GridView control aside to the familiar DataGrid control. This code supports the GridView control with few changes, related to the methods exposed by the control. VB.Net is used in the examples of this article.
Walk Through:
a) Create a template column in the datagrid with and add a Checkbox control to it.
Also, add a textbox to the page, as it will be required to illustrate how to use
data from the row hosting the checkbox triggering the CheckedChanged event

b) Set the AutoPostBack property of the checkbox as True.

c) The ItemCreated event of the datagrid or gridview is raised whenever an item is created. (It is raised at the time when the data is bound to the grid and also during the round trips), We will use it to map the checkbox's checkedchanged event for each row on these controls. This will ensure that anytime the user changes the checked state of the checkbox in the grid, a post back occurs and this event will be fired. So here we will add a handler for the CheckedChanged event of the checkbox.

It is important that you notice that Datagrids and Gridviews controls contain a collection of objects hosting the datarows, they are typified by their location, by example the data in their datarows does not belong to a unique type, as they are split between the Item and AlternateItems types, when you are mapping the checkedchanged event of your checkbox control, you should do so on both the Item and AlternateItems types, otherwise, it will not work in all rows; there are more types that you should be concerned with, they are EditItem and SelectedItem, include them when you want to trap the checkbox's checkedchanged event while editing or selectin datarows in the datagrid or gridview controls.

 
Private Sub DataGrid1_ItemCreated(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemCreated

Dim chk As CheckBox

' If you were using Vs.Net 2005 GridView controls, you should use
' the Select statement commented out below:
' Select Case e.Row.RowType
Select Case e.Item.ItemType
' Once again, with Vs.Net 2005's GridView, the property exposed
' is the RowType, so yur Case will be slighly different,
' Case DataControlRowType.DataRow
Case ListItemType.Item, ListItemType.AlternatingItem
' Finally, with Vs.Net 2005 you should use the Row propery when
' identifying the checkbox control in the row
' chk = CType(e.Row.FindControl("cbx"), CheckBox)
chk = CType(e.Item.FindControl("cbx"), CheckBox)
AddHandler chk.CheckedChanged, AddressOf chk_CheckedChanged
End Select
End Sub


d) Method to handle the Checked_Changed event.
Whenever the checked state of the checkbox changes, the following method will be invoked.
Here the object(sender) which invokes the CheckedChanged event is checkbox control and we
use this object to get the parent item of the checkbox.


Public Sub chk_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
' Convert the sender object to check box control
Dim chk As CheckBox = CType(sender, CheckBox)
' Get the parent Item of the checkbox control
Dim objItem As DataGridItem = CType(chk.Parent.Parent, DataGridItem)
' Show the ItemIndex of the Item
TextBox1.Text = objItem.ItemIndex.ToString()
' Now, if you want to work with any cell in the data row,
' the code that follow illustrates how to obtain the
' second data from the second column (cell)
Dim SelectedRow As Double = CDbl(TextBox1.Text)
Dim s As String = Datagrid1.Items(SelectedRow).Cells(2).Text
TextBox1.Text = s
End Sub


In VS.Net 2005 the above method becomes like the one below.


Public Sub chk_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
' Convert the sender object to check box control
Dim chk As CheckBox = CType(sender, CheckBox)
' Get the parent Item of the checkbox control
Dim objItem As GridViewRow = CType(chk.Parent.Parent, GridViewRow)
' Show the ItemIndex of the Item
TextBox1.Text = objItem.RowIndex.ToString()
Dim SelectedRow As Double = CDbl(TextBox1.Text)
Dim s As String = GridView1.Rows(CInt(SelectedRow)).Cells(2).Text
TextBox1.Text = s
End Sub


- Naison Garvasis Pekkattil


Comments

Author: Mahesh Raj07 Jun 2008 Member Level: Gold   Points : 1

This is very good information,Continue posting such useful articles.

Author: John Fernandez08 Jun 2008 Member Level: Gold   Points : 1

Very well written Article.Thanks for sharing this information.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: