noctonura
noctonura

Reputation: 13133

Variable inside of the markup for an asp:HyperLink NavigationUrl property?

I'm new to ASP.NET and can't figure out how to accomplish this...

My code (that needs fixing):

<asp:HyperLink runat="server"
          NavigateUrl="~/EditReport.aspx?featureId=<%= featureId %>" />

featureId gets defined as an integer in the backing code. I want href's like...

  /EditReport.aspx?featureId=2224

...but instead I am getting...

  /EditReport.aspx?featureId=<%= featureId %>

Upvotes: 0

Views: 2789

Answers (2)

roufamatic
roufamatic

Reputation: 18495

Most asp.net developers have run into this at one time or another. This is my favorite solution as it works for any server control:

http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx

Effectively you add this guy's brilliant little custom ASP.NET Expression Builder to your project, then rewrite the control to look like this:

<asp:HyperLink runat="server"
     NavigateUrl='<%$ CODE: String.Format("~/EditReport.aspx?featureId={0}", featureId) %>' />

As he explains it's cleaner than the <%# %> method as there's no ViewState involved. Note also the use of the single quotes which allow double quotes to be used inside the attribute.

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630627

You can do this without using the HyperLink control a bit easier:

<a href='<%=ResolveUrl("~/EditReport.aspx?featureId=" + featureId) %>'>Link</a>

Upvotes: 1

Related Questions