473,287 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

ASPNET2: Master Page File Reference Problem

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
Feb 3 '06 #1
3 2262
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
"Mark Olbert" <Ch*********@newsgroups.nospam> wrote in message
news:um********************************@4ax.com...
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

Feb 3 '06 #2
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.)

Feb 3 '06 #3
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/

"Mark Olbert" <Ch*********@newsgroups.nospam> wrote in message
news:um********************************@4ax.com...
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

Feb 4 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mark Olbert | last post by:
I'm starting to play around with VS2005 and ASP.NET 2 and have some questions. The typical pattern for a data-driven aspx page in v1.1 was data connection -> data adapter -> dataset -> bound...
9
by: Mark Olbert | last post by:
Has anyone found useful tutorials on how to deal with the shortcomings of master pages, specifically, the fact that they don't handle HTML file references when used to create web pages anywhere...
8
by: Mark Olbert | last post by:
There appears to be a REALLY annoying bug in VS2005: periodically when you're rebuilding an ASPNET2 website which uses App_Code stuff the compiler will fail to create the dynamic assembly or not be...
8
by: otto | last post by:
Hi, all: I have a problem with the inclusion of .js files in mu .aspx pages when using Master Pages. I try to explain it. If I make a web project without master pages I simply put in the head...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.