Master Page and Content Page Path in ASP.NET

I ran into this problem that i wanted to share.
i have a master page that i use in a content page but the content page is not in the same directory as the master page.
My master page contains "img" elements, when I browsed to the content page those images weren't displayed because they have invalid path.
I found the solution on the MSDN Site


"ASP.NET cannot modify URLs on elements that are not server controls. For example, if you use an img element on a master page and set its src attribute to a URL, ASP.NET will not modify the URL. In that case, the URL will be resolved in the context of the content page and create the URL accordingly.
In general, when working with elements on master pages, it is recommended that you use a server control, even for elements that do not require server code. For example, instead of using an img element, use an Image server control. That way, ASP.NET can resolve URLs correctly and you can avoid maintenance issues that might arise if you move the master or content page."


However this wasn't any good when it comes to the <body> element, the <body> element in my master page has a backgound image <body style="{backgound-image:">
i converted the body element to a server side control and changed the image url to be '~/images/bg.gif' but it didn't work, so i gave up and handeled it through the code

if (Request.CurrentExecutionFilePath.ToLower().Contains("/admin/"))
{
//not in the same folder
boddy.Style[HtmlTextWriterStyle.BackgroundImage] = "../images/bg.gif";

}
else
boddy.Style[HtmlTextWriterStyle.BackgroundImage] = "images/bg.gif";

Comments

Anonymous said…
Nice peace of code
Anonymous said…
You can also do this and just use one piece of code:

tagname.Style[HtmlTextWriterStyle.BackgroundImage] = ResolveClientUrl("~/images/whatever.jpg");
Anonymous said…
Here's an even easier way I found to do it (from http://techietweaks.blogspot.com/2007/04/designing-masterpages.html)

Popular posts from this blog

Documentum DQL Query Custom Data Extension For SQL Server Reporting Services

Portable Class Library Projects and MVVM Light

Using taco to create Ionic projects for the Windows platform