Specifying Root Relative Path in User Control

  • Thread starter Thread starter Guadala Harry
  • Start date Start date
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!
 
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!)
 
Back
Top