While reading ASP.NET 2.0 Unleashed by Stephen Walther, I found out a nice trick about relative paths in Master Pages.
If you are using Relative Paths and you are using ASP.NET controls in your master pages, then the paths are relative to the Master Page file, while if you are using HTML controls, the paths are relative to the content page and not master page.
So it is better to use ASP.NET Controls always, but if you still insist on using HTML controls with relative paths, then you can do a small trick and make the paths relative to the master page and not content page as follows:
<img src='<%=MasterUrl("Logo.gif") %>' alt="Website Logo" />
As you can see we are using an "img" tag with the source calling a server side method that fixes the path to be relative to the master page and not to the content page as follows:
public string MasterUrl(string url)
{
return string.Format("{0}/{1}", this.TemplateSourceDirectory, url);
}
The url is being formatted in such a way to start from the forlder where the master page is placed and append to it the path of the image!
Hope this tricks helps! Thanks Stephen!
Regards
Tags: ASP.NET 2.0 - Master Pages