Getting the DataKey Value in a GridView

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?
 
M

Manish

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
 
W

Wannabe

Sorry for the long time in replying, but I was on vacation. I will check this
out and get back to you. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top