0

我正在尝试将网格视图绑定到可折叠面板扩展器主体的中继器中。这是代码:

<!-- Collapsible panel extender body -->
<asp:Panel ID="pBody1" runat="server" CssClass="cpBody">
    <asp:Label ID="lblBodyText1" runat="server" />
    <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="Repeater2_ItemDataBound">
        <ItemTemplate>
            <asp:GridView ID="gvProduct" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="ID" />
                    <asp:BoundField DataField="name" HeaderText="Name" />
                    <asp:BoundField DataField="categoryName" HeaderText="Category" />
                    <asp:BoundField DataField="inventoryQuantity" HeaderText="Quantity" />
                </Columns>
            </asp:GridView>
        </ItemTemplate>
    </asp:Repeater>
</asp:Panel>

从后面的代码中,我试图遍历列表以获取类别名称。然后,我根据类别名称获取所有产品并在 gridview 中显示它们。

protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // This event is raised for the header, the footer, separators, and items.

    //Execute the following logic for Items and Alternating Items.
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        for (int count = 0; count < categoryList.Count; count++)
        {
            string category = categoryList[count].categoryName;
            List<ProductPacking> prodList = new List<ProductPacking>();
            prodList = packBLL.getAllProductByCategory(category);
            gvProduct.DataSource = prodList;
            gvProduct.DataBind();
        }
    }
}

但是,它告诉我 gvProduct 在当前上下文中不存在。我想知道如何将组件放入中继器中?还是我做错了?

更新部分。

这就是我为类别名称绑定标题的方式。我正在使用另一个中继器:

<asp:Label ID="lblCategory" Text='<%# DataBinder.Eval(Container.DataItem, "categoryName") %>' runat="server" />

从后面的代码中,在页面加载时,我得到了所有类别:

        if (!IsPostBack)
        {
            //Get all category and bind to repeater to loop
            categoryList = packBLL.getAllCategory();
            Repeater1.DataSource = categoryList;
            Repeater1.DataBind();
        }

对于在每个类别中显示产品的repeater2,我将其编辑为如下所示:

    protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        // This event is raised for the header, the footer, separators, and items.
        string category = e.Item.FindControl("categoryName").ToString();
        //Execute the following logic for Items and Alternating Items.
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            GridView gv = (GridView)e.Item.FindControl("gvProduct");
            if (gv != null)
            {
                List<ProductPacking> prodList = new List<ProductPacking>();
                prodList = packBLL.getAllProductByCategory(category);
                DataRowView drv = (DataRowView)e.Item.DataItem;
                gv.DataSource = prodList;
                gv.DataBind();
            }
        }
    }

但是,当我展开扩展器时,什么也没有出现。

4

1 回答 1

0

如果我没记错的话,你需要用来FindControl访问模板中的控件,比如

GridView oGV = e.Item.FindControl ( "gvProducts" ) as GridView;

您不能将 GridView 称为gvProducts因为没有具有该名称的单个 GridView 控件 - 为模板中的每个数据项创建一个不同的实例(具有不同的名称)。您需要当前行的实例。

这是一个MSDN 示例,展示了如何在数据绑定期间访问控件(该示例使用 a Label)。

于 2013-12-28T02:48:10.187 回答