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?

Re: Getting the DataKey Value in a GridView by Mark

Mark
Thu May 08 09:34:51 CDT 2008

"Wannabe" <Wannabe@discussions.microsoft.com> wrote in message
news:60CA5B12-682D-44E8-95CE-E4639D7303EC@microsoft.com...

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

protected void MyCheckBox_OnCheckedChanged(object sender, EventArgs e)
{
int intDataKey =
Convert.ToInt32(MyGridView.DataKeys[((GridViewRow)((CheckBox)sender).Parent.Parent).RowIndex].Value);
// rest of code
}


--
Mark Rae
ASP.NET MVP
http://www.markrae.net


RE: Getting the DataKey Value in a GridView by Manish

Manish
Sat May 10 03:05:00 CDT 2008

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

"Wannabe" wrote:

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

RE: Getting the DataKey Value in a GridView by Wannabe

Wannabe
Mon May 19 08:06:02 CDT 2008

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

"Manish" wrote:

> 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
>
> "Wannabe" wrote:
>
> > 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?