Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested Repeater Controls

Status
Not open for further replies.

kettch

Programmer
Joined
Mar 5, 2001
Messages
110
Location
US
Is it possible to have nested repeaters three layers deep?

I have gotten it to work with only two, and when I tried to get the third layer to work:


Code:
<asp:repeater id="rep" runat="server">
                <ItemTemplate>
                    <span style="BACKGROUND-COLOR: silver">
                        <%# DataBinder.Eval(Container.DataItem, "CustomerID") %>
                        &nbsp;
                        <%# DataBinder.Eval(Container.DataItem, "CompanyName") %>
                    </span>
                    <br />
                    <asp:repeater id="childRepeater" runat="server" datasource='<%# ((DataRowView)Container.DataItem)
                        .Row.GetChildRows("relation") %>' >
                        <itemtemplate>
                            &nbsp;&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "[\"OrderID\"]") %>&nbsp;&nbsp;&nbsp;&nbsp; 
                            &nbsp;&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "[\"OrderDate\"]") %>
                            
                            <br />
                            <asp:repeater id="Repeater1" runat="server" datasource='<%# ((DataRowView)Container.DataItem)
                                    .Row.GetChildRows("orderrel") %>' >
                                <itemtemplate>
                                    &nbsp;&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "[\"ProductID\"]") %>&nbsp;&nbsp;&nbsp;&nbsp; 
                                    &nbsp;&nbsp;&nbsp;<%# DataBinder.Eval(Container.DataItem, "[\"UnitPrice\"]") %>
                                    <br />
                                </itemtemplate>
                            </asp:repeater>
                        </itemtemplate>
                    </asp:repeater>
                </ItemTemplate>
            </asp:repeater>

I got this error on the datasource line:
Specified cast not valid


Code:
<asp:repeater id="Repeater1" runat="server" datasource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("orderrel") %>' >

By the way, all of this is built against the Northwind database for testing purposes. I can post my codebehind for all of the relations if you want. However, I checked it by commenting out the top layer of relations and only using the bottom two repeaters with orders and order details, and it works fine.

-Thanks
 
Code:
(DataRowView)Container.DataItem

is the only cast I see. If the error is referring to that cast, the DataItems in childRepeater are not DataRowViews. What else could they be?

BTW, there is an alternative way of nesting repeaters. Do not set the datasource for the child repeater in the page itself. Instead, set it during the parent repeater's ItemDataBound event. You will have more control over the objects involved.

Code:
parentRepeater_ItemDataBound(object source, RepeaterItemEventArgs e){

    DataRowView drv = (DataRowView)e.Item.DataItem;
    Repeater childRepeater;
    childRepeater = (Repeater)e.Item.FindControl("childRepeater");
    if(!(childRepeater==null)){
        childRepeater.DataSource = drv.Row.GetChildRows();
        childRepeater.DataBind();
    }
}

Hope that helps you find the answer.

Greetings,
Dragonwell
 
If the error is referring to that cast, the DataItems in childRepeater are not DataRowViews. What else could they be?

That is the big question, because if the nesting is only two layers deep, then the cast works the way it is.

Thanks for the tip, I'll try it out as soon as I can.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top