Getting the DataKey Value in a GridView

  • Thread starter Thread starter Wannabe
  • Start date Start date
W

Wannabe

I have a gridview that has a label field and a checkbox field in it. I have
the checkbox set to autopost when changed. How can I get the datakey value
that is assigned to that row when a checkbox is either checked or unchecked?
 
Hi,

You can try the following code to implement the same.

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
Dim chb As CheckBox = e.Row.Cells(2).FindControl("CheckBox1")
AddHandler chb.CheckedChanged, AddressOf check_changed
End If
End Sub
Public Sub check_changed(ByVal sender As Object, ByVal e As EventArgs)
Response.Write(Me.GridView1.DataKeys.Item(CType(CType(sender,
CheckBox).Parent.Parent, GridViewRow).RowIndex).Value)
End Sub

Regards,
Manish
www.componentone.com
 
Sorry for the long time in replying, but I was on vacation. I will check this
out and get back to you. Thanks.
 
Back
Top