7

Lets say I have a class structure that looks something like this:

public class A
{
    public string Poperty1 { get; set; }
    public string Poperty2 { get; set; }
    public List<B> Property3 { get; set; }
}

public class B
{
    public string Property4 { get; set; }
    public string Property5 { get; set; }
}

...and a couple of nested repeaters that look like this:

<asp:Repeater ItemType="A" runat="server">
    <ItemTemplate>
        <asp:Label Text="<%# Item.Property1 %>" runat="server" />
        <asp:Repeater runat="server" DataSource="<%# Item.Property3 %>" ItemType="B">
            <ItemTemplate>
                <asp:Label Text="<%# Item.Property4 %>" runat="server" />
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

How would I access Property2 from the second repeater?

Douglas Ludlow
  • 10,754
  • 6
  • 30
  • 54

4 Answers4

9

Well, from Accessing parent data in nested repeater, in the HeaderTemplate, I found the following solution. It's not the prettiest, but it works:

<%# ((Container.Parent.Parent as RepeaterItem).DataItem as A).Property2 %>
Community
  • 1
  • 1
Douglas Ludlow
  • 10,754
  • 6
  • 30
  • 54
  • I was hoping for a solution that involved some magic property that contained the strongly typed parent... but I haven't been able to find one... – Douglas Ludlow May 02 '13 at 18:02
3

You could use a generic Tuple as type for the inner repeater, and pass on the Item from the outer repeater:

<asp:Repeater ItemType="A" runat="server" ID="Rpt">
    <ItemTemplate>
        <asp:Label Text="<%# Item.Property1 %>" runat="server" />
        <asp:Repeater runat="server" 
            DataSource="<%#  Item.Property3.Select(innerItem => new Tuple<A,B>(Item, innerItem)) %>" 
            ItemType="System.Tuple<A,B>">
            <ItemTemplate>
                <asp:Label Text="<%# Item.Item2.Property4 %>" runat="server" />
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

Be aware that ReSharper will protest against using generics in ItemType!

Here is a different example, closer to something I was working on:

<asp:Repeater runat="server" ID="RptWeekNumbers" ItemType="System.Int32">
    <ItemTemplate>
        <asp:Repeater runat="server" 
            DataSource="<%# Enumerable.Range(1, 5).Select(day => new Tuple<int,int>(Item, day))%>" 
            ItemType="System.Tuple<int,int>">
            <ItemTemplate>
                WeekNumber: <%# Item.Item1 %>, 
                DayNumber: <%# Item.Item2 %>
                <br />
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>
andersh
  • 8,105
  • 6
  • 39
  • 30
1

When setting the DataSource for the inner repeater:

DataSource='<%# Eval("Property3") %>'

Note the single quote when setting the value.

mhall
  • 3,671
  • 3
  • 23
  • 35
0

You could assign the parent repeater's Item to a separate variable.

<script runat="server">
    A parentItem;
</script>
<asp:Repeater ItemType="A" runat="server">
    <ItemTemplate>
        <%# parentItem = Item %>
        <asp:Label Text="<%# Item.Property1 %>" runat="server" />
        <asp:Repeater runat="server" DataSource="<%# Item.Property3 %>" ItemType="B">
            <ItemTemplate>
                <asp:Label Text="<%# parentItem.Property4 %>" runat="server" />
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

Be aware, that the assignment in the databinding expression, will render in the html the ToString value of the expression. You might want to overwrite the ToString method of your type A, or alternatively wrap the assignment in a helper function that returns null.

Protector one
  • 6,926
  • 5
  • 62
  • 86