Wednesday, June 25, 2008

Master Page Image Display Problem and Solution in Asp.net

Master Page Image Display Problem and Solution in Asp.net

Master Page is creating problem when a page inside folder is trying to refer master page. The reason of problem is Master Page is using absolute image url, which changes when you try to refer same inside folder.




A simple solution to problem is to define relative path for images inside master page.

So if you have coded like following in your MasterPage File

<img src="Images/Logo.gif"/>



Than Replace it with,

<img src="<%= Page.ResolveUrl("~")%>Images/Logo.gif"/>

More about Asp.net Website Paths

10 comments:

Luke Foust said...

This is a very interesting solution. I have always solved this problem by doing this:

<img src="~/images/logo.gif" runat="server" />

I think your solution may have less overhead.

Anonymous said...

I used the same solution as Luke said above but I still had issues and yours worked perfect.

Thanks!

Amit Thk said...

I've put my head for 2 hours over two simple images 'plus.gif' and 'minus.gif'.... I wanted to toggle them using javascript. But in inside pages, this will not work.

I used this solution when assigning to javascript variables. Got it dear..... thanks a lot. :)

Disco Patrick said...

Great tip, thanks!

rohit said...

Thanks. Really helpful..

Unknown said...

I was struggling with this for several hours. When I ran my website from Visual Studio, everything worked. When I put it on the IIS server, the images didn't display. I used your fix and all is well!

Thank you very much.

Unknown said...

Did a miracle

Thank you very much for the solution
Worked like a wonder

ashra said...

i am new to asp.net i have create theme and using sideshow in jquery but the issue is when i build i am not able see the images .. example below

src="~images/img04.jpg" runat="server"

pls help..

loudwebsite said...

I am using nested master pages. The code works on the main master page but it does not produce the images on the nested master page. Is there a way to make this work on the nested page or avoid the rewriting on the nested master page completely?

Do the url rewriting values need to be in the nested web.config file as well?

Damodar said...

Hi DotNetGuts,
Thanks Worked like Charm!
I was getting cached url for image.
After using <%=Page.ResolveUrl("~")%>
it worked fine!
Thanks again.
Damo

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape