Here, I am going to talk about very common issue that coming with ASP.NET Master Page. If you are frequent visitor of any ASP.NET forum for sure you should see lot of thread regarding this issue.
Problem:
If you see above image, you can identify the project file structure inside VS IDE server explorer. The “my.master” page uses the “myimage.jpg” located in “image” folder. So, image URL should be like below.
The “default.aspx” page use “my.master” page and when you run the "default.aspx" page you can see the image without any problem. Because image path is correct as above. But when you run the “default.aspx” page that located inside the “admin” folder, you can’t see the image. Because image path is invalid due to its request another “image” folder inside the “admin” folder.
I hope, now you understand the problem behind this issue.
Solution 1:
You can overcome this issue by using ResolveClientUrl method. The ResolveClientUrl method automatically manages to exact path even inside the different folders. In this case ResolveClientUrl correctly return the image path.
Anyway, you better to remember this method only work at runtime. So you can’t see the image at application design time. Personally I feel dull when I can’t see images at design time.
Solution 2:
"Add runat="server" to "img" element"
I guess this is the very best technique for this issue. Even we only have to treat "img" element as server side control with runat=”server” tag. Then, ASP.NET always manages to locate the exact image path at design time and runtime. And also you better to remember you can simply overcome this problem with ASP.NET server side image control.
Download source code: MasterPageImagePath.zip
Labels: ASP.NET