Specifying Root Relative Path in User Control

G

Guadala Harry

I have a user control that I'm inserting into multiple pages. Part of the
HTML content in the user control is a path to an image:

<img src="../images/arrow.gif" width="11" height="8" border="0">

The image appears just fine when viewed from pages that exist one directory
level below the site root, but not when viewed from pages that exist in the
site root (which is as expected).

What I want to do is be able to specify the path to the image in a way that
it does not matter where in the site's directory the page exists (the page
into which the user control is being inserted).

Specifying the path like this does NOT work:
<img src="~/images/arrow.gif" width="11" height="8" border="0">

So, what can I do?

Thanks!
 
K

Karl Seguin

There are a handful of ways to solve this...

to get ~ to work, you need to stick a runat="server" on the img tag..

<img src="~/..." runat="server" ... />

you can also do somtehing like

<img src="<%=Request.ApplicationPath%>/images/arrow.gif" ... />

There are alternatives, such as creating your own control:

public class Image : HtmlImage{
protected override Render(HtmlTextWriter writer){
base.Src = Request.ApplicationPath + base.Src;
base.Render(writer)
}
}


Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top