【问题标题】:Repeater and Collapsible Panel Extender中继器和可折叠面板扩展器
【发布时间】:2013-12-28 02:34:03
【问题描述】:

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

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

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

【问题讨论】:

标签: c# for-loop repeater collapsiblepanelextender


【解决方案1】:

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

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

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

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

【讨论】:

  • 但是假设我在标题处有另一个标签,显示类别名称。如何获取标签内的文本并将产品的正确网格视图分配给正确的类别。查看我更新的部分。
  • @Gwen 对不起,我不明白你的意思。要访问中继器内的 GridView,您必须使用FindControl。这不是你的问题吗?
  • 哦,好的,谢谢。有用。我想我必须为不同的问题打开另一个线程。
  • @Gwen 是的,这通常是首选方式。无论如何,当我查看问题时,您没有更新。 :) 很高兴它有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-29
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多