?
Solved

How do I Set an Image url address at runtime?

Posted on 2007-04-10
3
Medium Priority
?
629 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 375 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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

752 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