ASPNET2: Master Page File Reference Problem

M

Mark Olbert

I have some img tags on a master page which reference files in a top-level directory. They look like this:

<img src="assets/test.gif" id="gnr" />

assets is a subdirectory of the website root.

My problem is this: this kind of path reference in a master page won't work when the page using the master is not in the root
folder:

root_page.aspx <- if this uses the master, the image file will be located and displayed correctly
subdirectory/subdir_page.aspx <- if this uses the master, it won't locate and display the image file

One solution I've used before is to change the image tag as follows:

<img runat="server" src="~/assets/test.gif" id="gnr" />

This lets the file be found...but unfortunately it makes the img tag not respond to CSS positioning attributes. Specifically, even
though the gnr id is defined as being absolutely positioned at the right margin (within relatively positioned div element), it still
floats on the left.

I'm pretty sure this is a bug.

But I'd love to hear how to work around it. Anyone got any ideas? Other than switching to <asp:image> tags; I got that to work
already.

- Mark
 
M

Mark Fitzpatrick

Mark,
One of the ways I get around it is to simply embed a <%
Response.Write(Request.ApplicationPath)); %> right in the src attribute like
so:
<img src="<% Response.Write(Request.ApplicationPath)); %>assets/test.gif"
id="gnr" />

You can also simply add the runat="server" attribute to the image tag
and it will then cause the server to process the location at runtime so it
will determine how to correctly reference it.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
S

Steven Cheng[MSFT]

Hi Mark,

You can also use the Page.ResolveUrl function to interpret the "~/xxxx"
url, e.g:

<img src='<%= ResolveUrl("~/Images/test.gif")%>' alt="test gif"/><br />

Hope also helps.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
C

clintonG

Its not a bug. This time it really is by design although the design imposes
a conflict with the CSS id selector. As used by the framework, the id
functions as an attribute the compiler needs to build the control tree when
the page is compiled.

The solution is to stop depending on the id selector to apply sytle. Use the
class selector. The id selector has no real value other than its intended
use to identify a once and only once instance in the page. No big deal as
inheritence, cascade and specificity are not affect one iota when not using
id selectors for style.

All the other inclie code block hacks are fine and well to know and have at
hand but simply using class selectors resolves this misperceived bug. Note
there is also something to be learned about class selectors when using
Themes which over-ride inline declarations...

// example when not using Themes which will over-ride the inline class
selector
<img src="~/assets/test.gif" id="gnr" class="ClassName" runat="server"/>

// example when using Themes
<img src="~/assets/test.gif" id="gnr" SkinID="ClassName" />
// .skin file entry
<asp:Image SkinID="ClassName" CssClass="ClassName" runat="server" />

There's more to discuss and learn of course but these points should at least
point you in the right direction with no hacks required noting again, they
are good to know and learn but sometimes not needed.

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top