如何创建与数据绑定参数声明RouteUrls? [英] How to create RouteUrls with databound parameters declaratively?

查看:141
本文介绍了如何创建与数据绑定参数声明RouteUrls?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用在ASP.NET 4新的路由功能(Web表单,而不是MVC)。现在我有一个asp:ListView控件被绑定到数据源。其中一个属性是客户端ID ,我想用从ListView的项目到另一个页面链接。在的Global.asax 我已经定义了一个路线:

I'm using the new Routing feature in ASP.NET 4 (Web forms, not MVC). Now I have an asp:ListView which is bound to a datasource. One of the properties is a ClientID which I want to use to link from the ListView items to another page. In global.asax I have defined a route:

System.Web.Routing.RouteTable.Routes.MapPageRoute("ClientRoute",
    "MyClientPage/{ClientID}", "~/Client.aspx");

,以便例如的http://服务器/ MyClientPage / 2 是一个有效的URL,如果客户端ID = 2的存在

so that for instance http://server/MyClientPage/2 is a valid URL if ClientID=2 exists.

在ListView的项目我有一个asp:超链接,这样我可以创建链接:

In the ListView items I have an asp:HyperLink so that I can create the link:

<asp:HyperLink ID="HyperLinkClient" runat="server" 
    NavigateUrl='<%# "~/MyClientPage/"+Eval("ClientID") %>' >
    Go to Client details
</asp:HyperLink>

虽然这工作我会preFER使用RouteUrl前pression使用RouteName而不是硬codeD路线。例如具有恒定的ClientID = 2我可以写:

Although this works I would prefer to use the RouteName instead of the hardcoded route by using a RouteUrl expression. For instance with a constant ClientID=2 I could write:

<asp:HyperLink ID="HyperLinkClient" runat="server" 
    NavigateUrl="<%$ RouteUrl:ClientID=2,RouteName=ClientRoute %>" >
    Go to Client details
</asp:HyperLink>

现在我想知道如果我能结合的路线前pression语法和数据绑定语法。基本上我喜欢&LT来代替恒定的 2 以上;%#的eval(客户端ID)%&GT; 。但是,在一个天真的方式做这...

Now I am wondering if I can combine the route expression syntax and the databinding syntax. Basically I like to replace the constant 2 above by <%# Eval("ClientID") %>. But doing this in a naive way...

<asp:HyperLink ID="HyperLinkClient" runat="server" 
    NavigateUrl='<%$ RouteUrl:ClientID=<%# Eval("ClientID") %>,RouteName=ClientRoute %>' >
    Go to Client details
</asp:HyperLink>

...不工作:&LT;%#的eval(客户端ID)%&GT; 不评价,但作为一个字符串。玩弄引号的几种口味也没帮到目前为止(分析器错误在大多数情况下)。

... does not work: <%# Eval("ClientID") %> is not evaluated but considered as a string. Playing around with several flavors of quotation marks also didn't help so far (Parser errors in most cases).

问:是否有可能在所有的什么,我想在这里实现吗?如果是,什么是正确的方法是什么?

Question: Is it possible at all what I am trying to achieve here? And if yes, what's the correct way?

感谢你在前进!

推荐答案

使用<一个href=\"http://msdn.microsoft.com/en-us/library/dd992877.aspx\">System.Web.UI.Control.GetRouteUrl:

VB:

<asp:HyperLink ID="HyperLinkClient" runat="server"  
    NavigateUrl='<%# GetRouteUrl("ClientRoute", New With {.ClientID = Eval("ClientID")}) %>' > 
    Go to Client details 
</asp:HyperLink>

C#:

<asp:HyperLink ID="HyperLinkClient" runat="server"  
    NavigateUrl='<%# GetRouteUrl("ClientRoute", new {ClientID = Eval("ClientID")}) %>' > 
    Go to Client details 
</asp:HyperLink>

这篇关于如何创建与数据绑定参数声明RouteUrls?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆