Solved

How do I Set an Image url address at runtime?

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now