Relative Path in master page for img tag
Asked Answered
L

2

5

I have a tag in a master page. I use this master page in many folders. So the src path of the tag should be different for each folder. Here is my code :

<img src="images/1.gif" />

and I have a folder named "images" and a folder named "Users". Master Page is in the root, but I use it in Users folder.

How can I set a dynamic address for src?

Limewater answered 1/10, 2009 at 20:53 Comment(0)
P
13

The easiest way would be to use an asp:Image tag. You need to add runat="server" in order to use ~ syntax to resolve your URLs.

<asp:Image ID="myImage" runat="server" ImageUrl="~/images/1.gif" />
Precancel answered 1/10, 2009 at 20:55 Comment(2)
Thanks. It's OK for <img> but another problem is for background-image attribute in <td> tag. like this : <td style="background-image: images/1.gif;"> </td> How can I solve it?Limewater
style="background-image: url(<%= ResolveUrl("~/images/1.gif") %>);"Mickelson
C
6

Just use <img runat="server" src="~/images/1.gif" />. This is documented here.

Childers answered 1/10, 2009 at 20:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.