# Solved: image in master page dissapears as soon as I save



## kinderyum (Jun 24, 2007)

Grrrr. I don't know why this has started happening! I create a new page from a master page (which contains an image) everything looks good until I go to save or preview it..the image dissapears! Please help!


----------



## kinderyum (Jun 24, 2007)

I just moved the Master Page from it's sub-directory into the root directory and all is well again.


----------



## kinderyum (Jun 24, 2007)

Well I spoke too soon, after I moved the Master Page into the root directory, the other pages started playing up. Anyways, after such googling I found the info below and fixed my problem by adding a runat="server" control in my image tag, which made it a server-side control (whatever that is! LOL)

Thought I'd post this info, incase it helps someone else.

Break Some URLs
Once again, let's think back to the beginning of the article. At runtime, the master page and the content page are in the same control hierarchy - the master page is essentially a user control inside the content page. At design time, however, the master page and content page are two different entities. In fact, the master page and content page may live in different directories. During design time, it's easy to put URLs and relative paths into our master pages, but we have to be careful when using relative paths. Take the following master page excerpt as an example:.










<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>

As long as the master page and the web form live in the same directory, the company logo will display in the browser. When the master page and web form live in different directories, the image will not appear. The browser requests knows nothing about master pages. The browser will interpret any relative paths it finds in the HTML as being relative to the webform. If our logo and master page files are in the root directory, but the web form is in a subdirectory, the browser will ask for logo.gif from the same subdirectory. The server will respond with a 404 (file not found) error.

The good news is, the ASP.NET runtime does provide a feature called "URL rebasing". The runtime will try to "rebase" relative URLs it finds on server-side controls inside a master page. This means the following relative path will work, no matter where the master page and web form live.









We've added a runat="server" attribute to the image tag, making the <img> a server-side control. When the master page file and logo are in the root directory, but the web form is in a subdirectory, the ASP.NET runtime will rebase the relative path it finds in the src attribute to point to the root of the website.

The following code will also work, because we are using a server-side Image object.

<asp:Image ImageUrl="logo.gif" runat="server" /> 
The ASP.NET runtime will also rebase paths it finds inside of the head tag. Take the following excerpt from a master page:

Untitled Page

If we request a webform from a subdirectory, the runtime will catch the href inside the link tag and rebase the URL to "../styles/styles.css". However, the runtime doesn't catch everything. If we included our style sheet with the following code, the runtime won't rebase the relative href.

Also, the runtime doesn't rebase URLs inside of embedded styles, and not all attributes are covered (the background attribute, for instance).





If you need to use a relative path in an area where the runtime does not provide the rebasing feature, you can compute a client side URL using ResolveClientUrl and passing a relative path. ResolveClientUrl, when called from inside a master page, will take into account the location of the master page, the location specified in the HTTP request, and the location specified by the relative path parameter to formulate the correct relative path to return.

> 
When working with image paths in embedded styles, it's often a good idea to move the style definition into a .css file. The ASP.NET runtime will rebase the path it finds inside a link tag, so we won't have any problems locating the stylesheet from any webform. Take the following style definition in a .css file:

body 
{
background-image:url('images\logo.gif');
} 
Relative paths are safe inside a .css file because the browser will always request logo.gif relative to the location of the stylesheet.


----------

