开发者

ASP.NET 4.0 Route expression builder inside Listview control

One of the features of ASP.NET 4.0 is Route Expression builder which allows you to set up hyperlinks like this:

<asp:HyperLink 
    runat="server" 
    NavigateUrl="<%$ RouteUrl:RouteName=producto开发者_运维技巧s,categoria=Cereales,id=2 %>" >Productos</asp:HyperLink>

Now I'm wondering if I can use this sort of syntax inside a ListView Control, I know is possible, but the tricky thing is that I want to genereate de route key value dynamically. So instead to write id=2 I would like to write id=<%# Eval("CategoryID") %> .

Can I do that?, if so, how should I write it.

Thanks for your help!


I have searched and searched for an example did not find a way to handle building routes in an expression builder. I finally figured out a way to do this in a gridview, maybe you can adapt? See my post at the following address where I answered my own question: How to use routing in web forms and HyperLinkFields of a GridView

I think an asp hyperlink control will not work because the controls are built before the Eval() is called, but an anchor is just text so it works within a template field.


Not sure whether you can do it with the Route ExpressionBuilder. Instead you can give an ID to the Hyperlink object in the .aspx file:

<asp:HyperLink ID="productHyperLink" runat="server">Productos</asp:HyperLink>

And set the NavigateUrl property in the Page_Load method in the .cs file:

protected void Page_Load(object sender, EventArgs e)
{
    int categoryId;
    string categoryName;

    // Get categoryId and categoryName from database

    RouteValueDictionary values = new RouteValueDictionary {
        { "id", categoryId.ToString() },
        { "categoria", categoryName } };
    VirtualPathData virtualPathData = RouteTable.Routes.GetVirtualPath(null, "productos", values);
    productHyperLink.NavigateUrl = virtualPathData.VirtualPath;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜