开发者

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

开发者 https://www.devze.com 2022-12-22 19:51 出处:网络
I\'m new to ASP.NET and can\'t figure out how to accomplish this... My code (that needs fixing): <asp:HyperLink runat=\"server\"

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 %>" />
开发者_JAVA百科

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


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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号