views:

566

answers:

2

Hello to all! I was wondering how, from an event on the child object inside a templatefield belonging to a gridview, I could reference the row.

[edit: apparently I'm wrong and parent is not null, weird stuff, is all.]

Thing is, there is a checkbox representing a boolean state in relation to a user's relation to an ID. I have to update that information manually, either by deleting the target row or inserting it, depending of the checkbox state.

The username I can get quite easily, the actual state too, through a function.

So, how do I get the ID that is in the row my gridview is referencing, from within that event?

I absolutely need that information, I can't use a workaround implying modifications to the database or the method used to do what I intend to do. I absolutely have to use a checkbox, and that event.

A: 

Maybe you could look for the Checkbox control in the RowCommand event of your Gridview? Pass the name of your checkbox to the FindControl method in the IF statement below

e.g

Protected Sub gvGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvGridView.RowCommand


  If CType(lgvr.FindControl("chkCheckBox"), CheckBox).Checked = True Then
     ' Some code in here
  Else
     ' Do something else here
  End If

End Sub

Also, if you need to check the Checkbox on each row then you might have to do something like this in the RowDataBound Event of the gridview.

Protected Sub gvGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGridView.RowDataBound

    Dim chkCheckBox as New CheckBox

    ' Pass the checkbox to a new CheckBox object
    chkCheckBox = CType(e.Row.Cells.Item(n).Controls(n), CheckBox)

    If chkCheckBox.Checked = True Then
        ' Some code in here
    Else
        ' Do something else here
    End If

End Sub

(For the Item n and Controls n you will have to work out those positions by using something like the Watch Window when you step through your code)

kevchadders
A: 

Seems the simplest solution I could use was to add an hiddenfield bound on the ID and refer to that instead of going knee deep in esoteric references. It became a simple matter of FindControl.

like (myChkBox.Parent.FindControl("myHiddenField") as HiddenField).Value

Maybe not as sexy as it could be, but still, it works, I guess.

MrZombie