繁体   English   中英

中继器和可折叠面板扩展器

[英]Repeater and Collapsible Panel Extender

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

<!-- 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();
            }
        }
    }

但是,当我扩展扩展器时,什么也没有显示。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM