Katherine Perotta
Katherine Perotta

Reputation: 125

Get dynamically created imagebutton ID (C#)

I have a table with a rows of information at the end end of each row, an imagebutton is created. I would like the user to be able to delete the row by clicking the image corresponding to the row but do not know how to get the ID of the row clicked.

This is my current code:

img = new ImageButton();
img.ImageUrl = "./Images/delete.png";
img.Width = 35;
img.Height = 35;
img.Attributes.Add("runAt", "server");
img.ID = thisReader["DocID"].ToString();
img.CausesValidation = false;
img.Click += new ImageClickEventHandler(deleteRow);
delete.Controls.Add(img);

And my event handler, which is currently empty. My ultimate goal is to get the value from
thisReader["DocID"].ToString() into the handler when clicked.

protected void deleteRow(object sender,  System.Web.UI.ImageClickEventArgs e)
{

}

Currently im setting the ID attribute to the ID I need but I don't think that is the correct way. From what im reading, I need to create a command argument and use that but am not sure how to proceed about this. Would appreciate some guidance.

Upvotes: 2

Views: 2227

Answers (1)

Adil
Adil

Reputation: 148150

You need to use the source parameter in the deleteRow event handler. You can type cast it to ImageButton and get its id.

protected void deleteRow(object sender,  System.Web.UI.ImageClickEventArgs e)
{
   string ID = ((ImageButton)sender).ID;
}

Upvotes: 1

Related Questions