using ~ (virtual path) with images

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
 
J

John Saunders

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.
 
P

Pete Beech

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
 
B

Brian Henry

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!
 
K

Ken Cox [Microsoft MVP]

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]
 
B

Brian W

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
 
B

Brian Henry

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
 
J

Jos

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.
 
P

Pete Beech

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
 
J

John Saunders

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>
 
G

George Ter-Saakov

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.
 
B

Brian W

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top