ImageButton.Click Event calling a method

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am assigning a dynamic imagebutton on a page and then assiging it a Click
event with the following code, to pass the string "test" to the DeleteDetail
method

testImgBtn.ID = "imgBtn" + loopnumber;
testImgBtn.ImageUrl = "imgs/delete_16.gif";
testImgBtn.AlternateText = "Delete this detail";
testImgBtn.Style["background-color"] = "#B4B4B4";
testImgBtn.Click += new ImageClickEventHandler(DeleteDetail("test"));

The method very simply is

protected void DeleteDetail(string id)
{
Response.Write(id);
}

However when i try to compile this, i get "Method name expected" error on
the Click line in the original code

Can anyone help with this one?
 
The .Click property expects the name of the method not an actual call
to the method

the click event handler has to take the form of: -

private void ImageButton1_Click(object sender,
System.Web.UI.ImageClickEventArgs e)

in your button creation code add this: -

testImgBtn.CommandArgument = "test";

then in the event hander method you can do this: -

ImageButton b = (ImageButton)sender;
string value = b.CommandArgument;
//value will equal "test"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top