Hyperlink control in user control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Y2dhbWJpbm8=?=

    Hyperlink control in user control

    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.asp x">Page2</aor 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">Pag e2</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
  • Mark Rae

    #2
    Re: Hyperlink control in user control

    "cgambino" <cgambino@discu ssions.microsof t.comwrote in message
    news:AFB96D8C-B6DC-4830-8638-2B9BBAA3F9B4@mi crosoft.com...
    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.asp x">Page2</aor 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">Pag e2</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">Pag e2</a>


    Comment

    • GroupReader

      #3
      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
      ResolveClientUr l() 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.


      Comment

      Working...