0

I am having problem with repeater and extensiblecollapsible panel extender in C#. What I am trying to do is inside the repeater1, I get all the category name and display as a label at extensible panel extender. As for the repeater2, I get all the product based on category and display in grid view. Here is how I set up my collapsible panel extender:

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

And for the code behind, this is how I get my category:

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

And for the repeater2, I must get the lblCategory to determine which category the products belong to:

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

However, when I run the page, it only shows up with the category but not the grid view inside each collapsible panel extender. I wonder am I doing in the wrong way.

Thanks in advance.

  • Is Repeater2 actually getting databound? You haven't written any `OnItemDataBound` stuff for Repeater1 either! – deostroll Dec 28 '13 at 09:03
  • But repeater1 works perfectly as in my repeater1 just to get the categoryName. Unfortunately nope, the repeater2 onDataBoundItem does not execute at all –  Dec 28 '13 at 09:06
  • Repeater2 should be assigned a datasource and databound somewhere in Repeater1's ItemDataBound event. There should be a similar code like the one you've written to bind your gridview, for your repeater2 to databind. – deostroll Dec 28 '13 at 09:44
  • So u mean I should move all the codes inside Repeater2_OnDataItemBound into Repeater1_OnDataItemBound? –  Dec 28 '13 at 09:55

1 Answers1

0

As per our discussion, you need to rewrite it entirely. So below is merely a suggestion:

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

In the code-behind:

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();
    }
}
deostroll
  • 11,661
  • 21
  • 90
  • 161
  • So that means I do not need the Repeater2 OnDataItemBound method anymore? Also what does your GetDataFromSomeSource() means? Because I am getting all the source inside Repeater2 DataItemBound() –  Dec 28 '13 at 10:05
  • Incorrect. You'd still need the OnItemDataBound method for Repeater2 , else, your gridview won't show up. `GetDataFromSource()` is simply some place holder for your logic that fetches the data. – deostroll Dec 28 '13 at 10:10
  • Oh okay. But what should I do inside GetDataFromSomeSource(). Sorry because I am really lost now –  Dec 28 '13 at 10:11
  • You can replace `GetDataFromSomeSource()` however you actually want it. The question you should ask yourself here is what is Repeater2 trying to bind to...? – deostroll Dec 28 '13 at 10:12
  • No no I mean I actually have no idea what data source I should bind to repeater2. Because what my repeater2 did is all inside the OnDataItemBound method already. –  Dec 28 '13 at 10:14
  • what was the intent of placing a Repeater2, and inside it a GridView? – deostroll Dec 28 '13 at 10:16
  • I put repeater2 because I cannot directly access the gridView inside the repeater1. And for the gridview, its to show all the products inside that category. Am I doing in the wrong way? –  Dec 28 '13 at 10:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44049/discussion-between-deostroll-and-gwen) – deostroll Dec 28 '13 at 10:19
  • Okay thanks. It works. I actually just need one repeater tho. Thanks a lot –  Dec 28 '13 at 10:50