473,385 Members | 1,384 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,385 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 2265
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.