Solved

How do I Set an Image url address at runtime?

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
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…

749 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