User control + relative path

A

Alex

Hi. I need some help on this.
I have a user control (a header) in my root directory, and all my pages use
that control. Then a created a folder (in my root directory) and put a new
page inside it, and that page use the same control, but this time the
control can't find it's images and resources because it is trying to find it
in the same folder instead of the root directory. Now, I know that I can
change the control image's relative paths for absolute paths, but then when
I upload my application to the real server I have to change all the paths
again. Of course I could make a copy of the control in my new folder but
then I have to copy all the images and everything too.
Is there some simple way of dealing with that?
Thanks,
Alejandro Penate-Diaz.
 
S

Scott Mitchell [MVP]

Alex said:
Hi. I need some help on this.
I have a user control (a header) in my root directory, and all my pages use
that control. Then a created a folder (in my root directory) and put a new
page inside it, and that page use the same control, but this time the
control can't find it's images and resources because it is trying to find it
in the same folder instead of the root directory. Now, I know that I can
change the control image's relative paths for absolute paths, but then when
I upload my application to the real server I have to change all the paths
again. Of course I could make a copy of the control in my new folder but
then I have to copy all the images and everything too.
Is there some simple way of dealing with that?

Alex, the best approach is to use either:

(a) Absolute paths (i.e., not relative ones)
(b) The ~ character in your Web controls

For (a), what this would mean, is rather than your paths being things like:

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

-or-

<img src="img/blue.jpg" />

You'd instead use fully qualified, absolute paths, like:

<img src="/Foo/Dir2/images/blue.gif" />

The problem with this is if you have a Web application on your local
server at /Foo, but this will be moved to / when you deploy it. Then
/Foo/Dir2/images will become /Dir2/images when you deploy...

To solve for this problem, use the second approach listed. What you can
do is add ~ in your Web controls, which will cause the path to be
resolved automagically at runtime. So, if you have an Image web control
you could specify the path like:

<asp:Image ImageUrl="~/images/blue.gif" />

This only works in Web controls (not static HTML markup).

hth

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
A

Alex

Thanks a lot. I think that I should use server controls instead of html
images, or better, I will use skmMenu instead of mine. ;-).
thanks, Alejandro Penate-Diaz.
 

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,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top