Question

I am trying to bind a grid view into a repeater for collapsible panel extender body. Here is the code:

<!-- 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>

And from the code behind, I am trying to loop thru the list to get category name. Then, I get all the products based on category name and display them in 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();
        }
    }
}

However, it told me that gvProduct does not exist in current context. I wonder how can I get the components inside a repeater? Or am I doing in the wrong way?

Updated Portion.

This is how I bind the header for category name. And I am using another repeater:

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

And from the code behind, at the page load, I get all the category:

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

And for repeater2 which shows the product in each category, I edited it to become like this:

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

However, when I expand the extender, nothing shows up.

Was it helpful?

Solution

If I remember right, you need to use FindControl to access controls that are part of templates, like

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

You cannot refer to the GridView as gvProducts because there's no single GridView control with that name - a different instance (with a different name) is created for each data item from the template. You need the instance for the current row.

Here's an MSDN example that shows how to access a control during data binding (the example uses a Label).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top