473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamically created ImageButton with events

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();
}

Nov 19 '05 #1
2 1952
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" wrote:
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();
}

Nov 19 '05 #2
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" wrote:
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" wrote:
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();
}

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Danny Bloodworth | last post by:
I have a usercontrol (login.ascx) that is very simple. It accepts input through two textboxes, and then the user clicks an Imagebutton to submit. I have a parent form that checks the page...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
2
by: JezB | last post by:
I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm having trouble attaching events to these. For example, adding an image button :- ImageButton nb = new ImageButton();...
1
by: Nathan Sokalski | last post by:
I want to create a pushbutton-like control on my webform that has an image on it. I used to think that this is what the ImageButton control was, but it seems to me that the ImageButton is nothing...
4
by: Jordan | last post by:
I need to dynamically add an ImageButton control to a user control and and do some server-side processing when the user clicks it. While I the ImageButton is added to the user control at runtime,...
2
by: Varangian | last post by:
Hi there, I'm using C# for a webapplication. In my page I'm building ImageButtons dynamically on Page_Load. However I want to access these Images at runtime for example when a user clicks a...
3
by: =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?= | last post by:
I've a situation where I'm creating controls programatically. I'm programatically creating table cells and rows. I've got an array of data, and I want to format the data on screen in rows of 4. ...
1
by: stimul8d | last post by:
okay; ASP. I have i listbox inside a user control which is dynamically created on page_init. I check for postback and only populate the datasource if it's false. regardless, i do this ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.