Seehyung Lee
Seehyung Lee

Reputation: 610

ASP.NET - Hyperlink relative path to current URL on ASPX page not in code behind

I tried this one but the href code is not generated

<asp:HyperLink ID="hlPrev" NavigateUrl="<%# this.Request.Url %>" runat="server" />

Is there any way to do this on aspx page not in the code behind?

Upvotes: 1

Views: 1233

Answers (1)

Win
Win

Reputation: 62290

If hlPrev is located outside of DataBound controls like GridView, there are two problems in our code -

  1. You want to use <%= %> instead of <%# %> which is used in DataBound controls.
  2. You cannot use <%= %> to set property of a server control. Basically, you cannot mix runat="server" with <%= %>.

Solution:

<a href="<%= Request.Url.ToString() %>">Click Me</a>

Upvotes: 4

Related Questions