【问题标题】:Build HyperLink within GridView for each row在 GridView 中为每一行构建 HyperLink
【发布时间】:2014-11-11 21:12:24
【问题描述】:

我有一个名为“BasketGrid”的 GridView,其中包含有关用户选择购买的产品的信息。我在产品选项列中添加了一个名为“EditProdOpts”的超链接,以便用户可以在完成结帐过程之前返回产品页面进行更改(如果需要,该 URL 不在数据库中,所以我无法做到) GridView 中的 HyperLinkField)。

我目前有一个功能设置,应该为超链接设置正确的 url,但它只能部分工作。目前,它将 HyperLink 的每个实例设置为 GridView 中最后一个产品的 url。不过,我不确定我错过了什么......

这是函数:

    protected string BuildLink(object dataItem)
    {
        string ProductLink = "";
        foreach (GridViewRow basketRow in BasketGrid.Rows)
        {
            BasketItem item = (BasketItem)dataItem;
            if ((item.OrderItemType == OrderItemType.Product))
            {
                HyperLink optionsLink = (HyperLink)basketRow.FindControl("EditProdOpts");
                if (optionsLink != null)
                {
                    optionsLink.NavigateUrl = string.Format("~/Admin/orders/Create/CreateOrderAddProduct.aspx?Action=Edit&UID={0}&LineID={1}&ProdId={2}", _UserId, item.BasketItemId, item.ProductId);
                    ProductLink = optionsLink.NavigateUrl;
                }
            }
        }
        return ProductLink;
    }

这是超链接在 GridView 中的位置:

                    <asp:TemplateField HeaderText="Item">
                        <HeaderStyle CssClass="columnHeader" HorizontalAlign="left" VerticalAlign="top" />
                        <ItemStyle HorizontalAlign="Left" />
                        <ItemTemplate>
                            <uc:BasketItemDetail ID="BasketItemDetail1" runat="server" BasketItem="<%# Container.DataItem %>" LinkProducts="false" /><br /><%# Container.DataItem %>
                                <asp:PlaceHolder ID="EditOptPanel" runat="server" Visible='<%# ShowOptionsLink(Container.DataItem) %>'>
                                    <asp:HyperLink ID="EditProdOpts" runat="server" Text="Edit Options" NavigateUrl='<%# BuildLink(Container.DataItem) %>' />
                                </asp:PlaceHolder>
                        </ItemTemplate>
                    </asp:TemplateField>

【问题讨论】:

    标签: c# gridview hyperlink


    【解决方案1】:

    您不需要遍历 GridView 的行。

    您的函数将为每一行调用一次。

    你可能根本不需要你的函数。查看 DataNavigateUrlFormatString 属性:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.datanavigateurlformatstring%28v=vs.110%29.aspx

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 2016-03-17
      相关资源
      最近更新 更多