Protector one
Protector one

Reputation: 7271

Set an ASP.NET HyperLink NavigateUrl to a relative path?

When you try to set a HyperLink Control's NavigateUrl property to a relative path, e.g.:

pages/myPage.aspx

ASP.NET will resolve the relative URL to an absolute one.

How do I get a proper relative URL in the generated HTML?

Interestingly, relative URLs with a leading slash, are rendered as-is, without being resolved.

Upvotes: 1

Views: 2818

Answers (1)

Protector one
Protector one

Reputation: 7271

Instead of NavigateUrl, set the Control's href attribute:

<asp:HyperLink runat="server" href="pages/myPage.aspx">test</asp:HyperLink>

or in code-behind:

myHyperLinkId.Attributes["href"] = "pages/myPage.aspx";

Upvotes: 3

Related Questions