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>

コードビハインドから、リストをループしてカテゴリ名を取得しようとしています。次に、カテゴリ名に基づいてすべての製品を取得し、グリッドビューに表示します。

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

そして、各カテゴリーの商品を表示するリピーター2は、以下のように編集しました。

    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