Benny Ae Benny Ae - 3 months ago 10
ASP.NET (C#) Question

assign event to ImageButton.Click from code behind

I want to assign events to each ImageButton from code behind, but i can not find out how to write a proper one.

foreach (string one in urls)
{
ImageButton temIBTN = new ImageButton();
temIBTN.Attributes.Add("Width","265px");
temIBTN.Attributes.Add("Width", "144px");
temIBTN.ImageUrl = one;
temIBTN.Click +=
new EventHandler(setBigPic(sender, e, one));//<---don't know how...
}



protected void setBigPic(object sender, ImageClickEventArgs e,string url)
{

img_Big.ImageUrl = url;

}

Answer

Your method signature for the event handler was wrong, and you need to get the ImageUrl from the button that's firing the event. This should do it:

foreach (string one in urls)
{
    ImageButton temIBTN = new ImageButton();
    temIBTN.Attributes.Add("Width","265px");
    temIBTN.Attributes.Add("Width", "144px");
    temIBTN.ImageUrl = one;
    temIBTN.Click += setBigPic;
}

protected void setBigPic(object sender, ImageClickEventArgs e) 
{
   img_Big.ImageUrl = ((ImageButton)sender).ImageUrl;
}