Solved

How do I Set an Image url address at runtime?

Posted on 2007-04-10
3
624 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 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