R – Get Id of a row from an image button in a gridview in vb.net


I have a gridview and in that gridview i created a list of imagebuttons programmatically with an addhandler as follows:

Dim deletecshr As New ImageButton
deletecshr.ImageUrl = "\images\bttnDeletemini.gif"
deletecshr.ToolTip = "This Will Delete All Cashiers"
AddHandler deletecshr.Click, AddressOf deletecshr_Click
deletecshr.Attributes.Add("onclick", "javascript: if(confirm('Are you sure you want to delete all of these cashiers?')==false) return false;")

    If e.Row.Cells.Count > 2 And e.Row.RowType <> DataControlRowType.Header And e.Row.RowType <> DataControlRowType.Footer Then
    End If

my issue is getting the value of the row of the clicked imagebutton in the click handler. How do i get that?

Best Solution

I'd recommend assigning

imageButton.CommandName = "Delete"
imageButton.CommandArgument = Your_Row_ID_You_Want_To_Get

and then using the imageButton.OnCommand to do your work.

Edit: Here are the steps to follow:

  1. Create your image button in RowCreated
  2. Assign the CommandName and CommandArgument of your image button in RowDataBound
  3. In RowCommand check the image buttons CommandArgument for that row and you should see the ID that you previously stored there.
Related Question