1

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;

}

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.