如何使用数据绑定参数声明地创建RouteUrls? [英] How to create RouteUrls with databound parameters declaratively?
问题描述
ClientID
,我想用于从ListView项目链接到另一个页面。在 global.asax
我已经定义了一条路线: System.Web .Routing.RouteTable.Routes.MapPageRoute(ClientRoute,
MyClientPage / {ClientID},〜/ Client.aspx);
所以例如 http:// server / MyClientPage / 2
是一个有效的URL,如果ClientID = 2存在。
在ListView项目中,我有一个asp:HyperLink,以便我可以创建链接: p>
< asp:HyperLink ID =HyperLinkClientrunat =server
NavigateUrl ='<%# / MyClientPage /+ Eval(ClientID)%>'>
转到客户详细信息
< / asp:HyperLink>
尽管这样可以使用RouteUrl表达式,但我更愿意使用RouteName而不是硬编码的路由。例如对于常量ClientID = 2,我可以写:
< asp:HyperLink ID =HyperLinkClientrunat =server
NavigateUrl =<%$ RouteUrl:ClientID = 2,RouteName = ClientRoute%> >
转到客户详细信息
< / asp:HyperLink>
现在我想知道我是否可以组合路由表达式语法和数据绑定语法。基本上我喜欢用<%#Eval(ClientID)%>
替换常量 2
。但是以一种天真的方式这样做...
< asp:HyperLink ID =HyperLinkClientrunat =server
NavigateUrl ='<%$ RouteUrl:ClientID =<%#Eval(ClientID)%>,RouteName = ClientRoute%>'>
转到客户详细信息
< / asp:HyperLink>
...不工作:<%#Eval(ClientID )%>
未被评估,但被视为字符串。使用多种引号的玩法也没有帮助到目前为止(大多数情况下解析器错误)。
问题:是否可能在所有我试图在这里实现?如果是的话,那是正确的方法?
提前谢谢!
使用 System.Web.UI.Control.GetRouteUrl :
VB:
< asp:HyperLink ID =HyperLinkClient runat =server
NavigateUrl ='<%#GetRouteUrl(ClientRoute,New with {.ClientID = Eval(ClientID)})%>'>
转到客户详细信息
< / asp:HyperLink>
C#:
code>< asp:HyperLink ID =HyperLinkClientrunat =server
NavigateUrl ='<%#GetRouteUrl(ClientRoute,新{ClientID = Eval(ClientID)})% >'>
转到客户详细信息
< / asp:HyperLink>
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");
so that for instance http://server/MyClientPage/2
is a valid URL if ClientID=2 exists.
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>
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>
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>
... 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?
Thank you in advance!
Use 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屋!