Board index » Web Programming » Hyperlink control in user control

Hyperlink control in user control

Web Programming291
Hello,



I am having what I think is a problem with hyperlinks in user controls. My

usercontrol is in my Controls/NavControls directory. The page that I have

consuming the control is in my Pages/PageInfo/ directory.



My control, PageNav.ascx has an asp:hyperlink control on it that sets the

NavigateUrl property dynamically. I want to set it to a local reference,

something like: <a href="page2.aspx">Page2</a>or having it generate to:

http://mysite/Pages/PageInfo/page2.aspx would work as well.



However, it seems that the hyperlink is generating is to the Control's

directory, instead of the consuming page directory.. ie it is rendering to <a

href="http://mysite/Controls/NavControls/page2.aspx">Page2</a>... which is

not a valid page.



Does anyone know if there are any properties I can set to make this work

like I want? Or is there some code involved that I'd have to put in.



Thanks


-
 

Re:Hyperlink control in user control

"cgambino" <cgambino@discussions.microsoft.com>wrote in message



Quote
I am having what I think is a problem with hyperlinks in user controls.

My

usercontrol is in my Controls/NavControls directory. The page that I

have

consuming the control is in my Pages/PageInfo/ directory.



My control, PageNav.ascx has an asp:hyperlink control on it that sets the

NavigateUrl property dynamically. I want to set it to a local reference,

something like: <a href="page2.aspx">Page2</a>or having it generate to:

http://mysite/Pages/PageInfo/page2.aspx would work as well.



However, it seems that the hyperlink is generating is to the Control's

directory, instead of the consuming page directory.. ie it is rendering to

<a

href="http://mysite/Controls/NavControls/page2.aspx">Page2</a> ... which

is

not a valid page.



Does anyone know if there are any properties I can set to make this work

like I want? Or is there some code involved that I'd have to put in.



<a href="../../Pages/PageInfo/page2.aspx">Page2</a>





-

Re:Hyperlink control in user control

I agree with Mark Rae, but to elaborate a bit more....



The behavior that you are describing is by design and should be what

you want. You want all the paths to be relative from the User

Control, and *not* from the containing page. That way you can re-use

your user control on multiple pages without breaking all the paths.

Sound right?



So, what people normally do is use the ".." operator to back up one

directory as in href="..\images\myimage.gif".



See also: the tilde "~" as used in paths and the "ResolveUrl() and

ResolveClientUrl() methods". You shouldn't have to use these though.

You can also start a path with "/" to say that your are starting from

the root of your website. The root might change, though, if you

deploy to different servers/environments.





-