开发者

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 %>" />
开发者_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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜