views:

2123

answers:

2

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
        e.Row.Cells(3).Controls.Add(deletecshr)
    End If

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

+3  A: 

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.
Adam Markowitz
for a similar example see: http://forums.asp.net/p/1266962/2383867.aspx
balint
I should have been more specific. I looked at the example but i can't get the Id because the button is not on my source code because the buttons are generated programmatically on Gridview_rowcreated.that's the reason I had to create and Addhandler Thank you for your quick response.
Eric
Try using the GridView RowDataBound event to create your bindings to the row ID for your buttons instead as you won't be able to access that info in the RowCreated.
Adam Markowitz
Adam that worked great. I couldn't use the addhandler I created but I am able to use Gridview_RowCommand to write my commands. But how do i get the id of that row selected to delete that record in the database. I've tried Gridview.SelectedDataKey and i set the datakeynames and I've tried Gridview.SelectedValue...neither have worked.Thanks again.
Eric
Edited with steps. Follow the steps and it should work.
Adam Markowitz
Adam, Thanks for your great help. I tried to vote for you but i don't have a high enough reputation. Your steps did help me.
Eric
No worries. Glad to be of help. Good luck with the project!
Adam Markowitz
A: 

Hi Adam, I do need your help. I have a gridview which already have columns like edit & delete . I want to know the ID of the row which I am deleting/editing. I am using Asp.net [C#]. Actually I need this ID to get the reference of that Row. Please help me.

Thanks.