Dynamically created ImageButton with events

G

Guest

I have a button on a form that creates multiple ImageButtons; I want each
ImageButton's click event to then redirect to another URL. From doing some
research, I understand this has to do with ImageButtons (and their handlers)
not being re-assigned on postback, but how do I accomplish the above?

....
ImageButton ib = new ImageButton();
ib.Width = 100;
ib.Height = 80;
ib.ImageUrl = @"http://mysite.com/images/image1.jpg";
ib.ID = "ib1";
ib.Click += new System.Web.UI.ImageClickEventHandler(this.ib_Click);
ib.Attributes["runat"]="server";
ib.EnableViewState = true;
PImages.Controls.Add(ib);
....

private void ib_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Response.Redirect(@"http://www.microsoft.com");
Response.End();
}
 
G

Guest

Thanks. I simplified the example somewhat, however, there are a few
calculations that I need to do in the handler so it is not really just a
redirect. Any suggestions on how to do this?

Bruce

bchandley said:
Bruce,
If redirecting onclick is your only requirement, do not post back. use
clientside script:

ImageButton ib = new ImageButton();
ib.Width = 100;
ib.Height = 80;
ib.ImageUrl = @"http://mysite.com/images/image1.jpg";
ib.ID = "ib1";
ib.Attributes["onclick"]=@"window.loaction='http://www.microsoft.com'";
ib.EnableViewState = true;
PImages.Controls.Add(ib);


Brian





Bruce said:
I have a button on a form that creates multiple ImageButtons; I want each
ImageButton's click event to then redirect to another URL. From doing some
research, I understand this has to do with ImageButtons (and their handlers)
not being re-assigned on postback, but how do I accomplish the above?

...
ImageButton ib = new ImageButton();
ib.Width = 100;
ib.Height = 80;
ib.ImageUrl = @"http://mysite.com/images/image1.jpg";
ib.ID = "ib1";
ib.Click += new System.Web.UI.ImageClickEventHandler(this.ib_Click);
ib.Attributes["runat"]="server";
ib.EnableViewState = true;
PImages.Controls.Add(ib);
...

private void ib_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Response.Redirect(@"http://www.microsoft.com");
Response.End();
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top