Solved

How do I Set an Image url address at runtime?

Posted on 2007-04-10
3
626 Views
Last Modified: 2013-12-17
I have an image control on my webpage. I set the image to be displayed at runtime. What I also want to do is to make this image a link, and to set the url address at runtime. How do I do this?

Im Using C#.NET, ASP.NET 2.0
0
Comment
Question by:rangers99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:HappyFunBall
ID: 18883755
You can either wrap the img control within an <a href> control, set its ID, and change the href in the codebehind, or you can use the ImageButton control in ASP.NET and set its onclick and ImageURL properties.

More info on ImageButton control:
http://www.dotnetjunkies.com/QuickStartv20/aspnet/doc/ctrlref/standard/imagebutton.aspx
0
 

Author Comment

by:rangers99
ID: 18884127
Hi
Thanks. Can you explain how I change the href from the code-behind (for an image control)
0
 
LVL 9

Accepted Solution

by:
HappyFunBall earned 125 total points
ID: 18884224
If you are using Visual Studio, just open the code-behind .aspx.cs file and add code to the Page_Load event.  

MyImageButtonControl.ImageURL = "your image src goes here";

Then you have a few options.  If you want to post back when the button is clicked, you can handle the OnClick event of the button.  The easiest way to do that is to double-click on the control in designer view, and Visual Studio automatically adds the handler for the OnClick event.  Or you can manually add it:

void ImageButton_Click(object sender, ImageClickEventArgs e)
      {
         Response.Redirect("The url you want to send them to goes here");
      }

and make sure to wire it up to the control by adding the OnClick attribute to the <asp:ImageButton> tag

<asp:ImageButton id="imagebutton1" runat="server"
           AlternateText="ImageButton 1"
           ImageAlign="left"
           ImageUrl="images/pict.jpg"
           OnClick="ImageButton_Click"/>

OR if you don't want a postback to occur, you can use the OnClientClick event and use Javascript to handle the link.  In that case add this to the Page_Load event:

MyImageButtonControl.OnClientClick = "location.href='my url goes here';"
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question