using ~ (virtual path) with images

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

I made a header control in asp.net and it references images in the /images/
folder in the virtural path's root folder... How can I get the header
control to show the images at any level of the web site? i got the controls
to load using the ~/tempates/ctrlHeader thing in the references section of
the page that uses them, but I need that same idea in the actual control so
it knows to refrence the image folder no mater where the page its doing it
from is located in the site... how can i do this? is there a way to use the
~ in the img src? thanks

example structure of this

/ root
|--- images
|--- controls for header etc...
| -- main page.html
|--- new pages in folder
| --- sub page 1.. references control header and img
 
Brian Henry said:
I made a header control in asp.net and it references images in the /images/
folder in the virtural path's root folder... How can I get the header
control to show the images at any level of the web site? i got the controls
to load using the ~/tempates/ctrlHeader thing in the references section of
the page that uses them, but I need that same idea in the actual control so
it knows to refrence the image folder no mater where the page its doing it
from is located in the site... how can i do this? is there a way to use the
~ in the img src? thanks

example structure of this

/ root
|--- images
|--- controls for header etc...
| -- main page.html
|--- new pages in folder
| --- sub page 1.. references control header and img

Perhaps you should give an example of what you're talking about.
 
If I understand your situation right, it should work if you make sure the
<img> tag runs on the server, and then use the ~ notation to get to the
images subdir,

e.g. <img src="~/images/pic.jpg" runat=server>

HTH,
Pete Beech
 
thanks! that works great for img tag's.. is there anyway to also do this
with backgrounds of tables? runat=server doesnt seem to work on td tags with
background = "~/images/img.gif"

thanks!
 
Hey Brian,

Don't forget the ASP.NET Table control...

<asp:Table id="Table1" runat="server" BorderWidth=1
BackImageUrl="~/images/floor.jpg">
<asp:TableRow>
<asp:TableCell>Hello</asp:TableCell>
<asp:TableCell></asp:TableCell>
</asp:TableRow>
<asp:TableRow></asp:TableRow>
<asp:TableRow></asp:TableRow>
</asp:Table>

Ken
Microsoft MVP [ASP.NET]
 
No, Brian, it won't work with tables. You can do something like this...

<table background='<%=ResolveUrl("~/images/image.jpg")%>'>
.... rest of table goes here


HTH
Brian W
 
thanks, all your comments helped a lot

Brian W said:
No, Brian, it won't work with tables. You can do something like this...

<table background='<%=ResolveUrl("~/images/image.jpg")%>'>
... rest of table goes here


HTH
Brian W
 
Brian Henry said:
I made a header control in asp.net and it references images in the /images/
folder in the virtural path's root folder... How can I get the header
control to show the images at any level of the web site? i got the controls
to load using the ~/tempates/ctrlHeader thing in the references section of
the page that uses them, but I need that same idea in the actual control so
it knows to refrence the image folder no mater where the page its doing it
from is located in the site... how can i do this? is there a way to use the
~ in the img src? thanks


Yes. Add the attribute runat="server" to the <img> tag.
Of course, this will only work in .aspx files, not in .htm or .html files.
 
Hi Rick,
I had a quick look at your blog entry - I think ResolveURL will do the same
as your function, with regard to ~'s at the start of the URL.

Pete
 
Brian W said:
No, Brian, it won't work with tables. You can do something like this...

<table background='<%=ResolveUrl("~/images/image.jpg")%>'>
... rest of table goes here

Why wouldn't the following work:

<table runat="server" background="~/images/image.jpg">
....
</table>
 
Why should it.
It works only in a handful amount of methods.
If some processing of the path is required then it works. If it's just a
property you set then it does not.

George.
 
Actually, it has to do with how the HTML server control was implemented.
Whoever wrote the code for System.Web.UI.HtmlControls.HtmlTable chose not to
resolve the path before rendering.


Regards
Brian W
 
Back
Top