Hyperlink control in user control


G

Guest

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
 
Ad

Advertisements

M

Mark Rae

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

Advertisements

G

GroupReader

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.
 

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

Top