Gridview不会出现在Repeater中

时间:2013-12-28 08:06:35

标签: c# asp.net ajax repeater collapsiblepanelextender

我在C#中遇到中继器和可扩展可缩放面板扩展器的问题。我想要做的是在repeater1内部,我获得所有类别名称并在可扩展面板扩展器上显示为标签。至于repeater2,我根据类别得到所有产品并在网格视图中显示。以下是我设置可折叠面板扩展器的方法:

<asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <!-- COLLAPSIBLE PANEL EXTENDER -->
                        <asp:Panel ID="pHeader1" runat="server" CssClass="cpHeader">
                            <!-- Collapsible panel extender header -->
                                        <asp:Label ID="lblCategory" Text='<%# DataBinder.Eval(Container.DataItem, "categoryName") %>' runat="server" />
                                        <asp:Label ID="lblHeaderText1" runat="server" />
                                        <asp:Image ID="imgArrows1" Text="IMAGE" runat="server" />
                        </asp:Panel>
                        <!-- 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>
                        <asp:CollapsiblePanelExtender ID="cpe1" runat="server" TargetControlID="pBody1" CollapseControlID="pHeader1"
                            ExpandControlID="pHeader1" Collapsed="true" ImageControlID="imgArrows1"
                            CollapsedImage="~/Images/downarrow.jpg"
                            ExpandedImage="~/Images/uparrow.jpg" TextLabelID="lblHeaderText1" CollapsedText="Show"
                            ExpandedText="Hide" CollapsedSize="0" ExpandedSize="200"
                            ScrollContents="true">
                        </asp:CollapsiblePanelExtender>
                    </ItemTemplate>
                </asp:Repeater>

对于背后的代码,这是我获得我的类别的方式:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            categoryList = packBLL.getAllCategory();
            Repeater1.DataSource = categoryList;
            Repeater1.DataBind();
        }
    }

对于repeater2,我必须得到lblCategory来确定产品属于哪个类别:

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

        Label lblCategory = (Label)e.Item.FindControl("lblCategory");
        string category = lblCategory.Text;
        //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") as GridView;
            if (gv != null)
            {
                List<ProductPacking> prodList = new List<ProductPacking>();
                prodList = packBLL.getAllProductByCategory(category);
                for (int i = 0; i < prodList.Count; i++)
                {
                    DataRowView drv = (DataRowView)e.Item.DataItem;
                    gv.DataSource = drv[prodList[i].id]; ;
                    gv.DataBind();
                }
            }
        }
    }

但是,当我运行页面时,它只显示类别,但不显示每个可折叠面板扩展器内的网格视图。我想知道我做错了。

提前致谢。

1 个答案:

答案 0 :(得分:0)

根据我们的讨论,您需要完全重写它。所以下面只是一个建议:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>
        <!-- COLLAPSIBLE PANEL EXTENDER -->
        <asp:Panel ID="pHeader1" runat="server" CssClass="cpHeader">
            <!-- Collapsible panel extender header -->
                        <asp:Label ID="lblCategory" Text='<%# DataBinder.Eval(Container.DataItem, "categoryName") %>' runat="server" />
                        <asp:Label ID="lblHeaderText1" runat="server" />
                        <asp:Image ID="imgArrows1" Text="IMAGE" runat="server" />
        </asp:Panel>
        <!-- Collapsible panel extender body -->
        <asp:Panel ID="pBody1" runat="server" CssClass="cpBody">
            <asp:Label ID="lblBodyText1" runat="server" />
            <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>
        </asp:Panel>
        <asp:CollapsiblePanelExtender ID="cpe1" runat="server" TargetControlID="pBody1" CollapseControlID="pHeader1"
            ExpandControlID="pHeader1" Collapsed="true" ImageControlID="imgArrows1"
            CollapsedImage="~/Images/downarrow.jpg"
            ExpandedImage="~/Images/uparrow.jpg" TextLabelID="lblHeaderText1" CollapsedText="Show"
            ExpandedText="Hide" CollapsedSize="0" ExpandedSize="200"
            ScrollContents="true">
        </asp:CollapsiblePanelExtender>
    </ItemTemplate>
</asp:Repeater>

在代码隐藏中:

public void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs  e)
{
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {   
        GridView gvProduct = (GridView)e.Item.FindControl("gvProduct");
        DataRowView drv = (DataRowView)e.Item.DataItem;
        gvProduct.DataSource = drv[prodList[i].id];
        gvProduct.DataBind();
    }
}