Access to parent data in a nested strongly typed relay

Suppose 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 few 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 do I access Property2 from a second repeater?

+7
source share
3 answers

Well, from Having accessed the parent data in a nested repeater, in the HeaderTemplate , I found the following solution. This is not the most beautiful, but it works:

 <%# ((Container.Parent.Parent as RepeaterItem).DataItem as A).Property2 %> 
+8
source

You can use the generic Tuple type for the internal repeater and pass the element from the external 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> 

Remember that ReSharper will protest against the use of generics in ItemType!

Here is another 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> 
+3
source

When installing a data source for an internal repeater:

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

Note the single quotation mark when setting the value.

+1
source

All Articles