Every once in a while a presentation comes along that requires multiple nested repeaters. The majority of times I’ve encountered this, there is something special that needs to be done to the last item in the inner repeater. With one repeater, this sort of thing is trivial. For example, I may use this logic to set the html element’s class:
1 2 3 |
class="<%# Container.ItemIndex == ((DataListObject)Repeater.DataSource).Count - 1 ? "lastItem": "innerItem" %>" |
Once you start nesting repeaters, this approach breaks since you can’t access the inner repeater control’s ID in this way. I don’t want to use the DataBound event in this case because I like keeping the class names inline in the code-front so designers can find them quickly. Instead, I access the parent’s DataItem like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<asp:Repeater runat="server" id="OuterRptr"> <ItemTemplate> <asp:Repeater runat="server" id="InnerRptr" DataSource="<%# Container.DataItem %>"> <ItemTemplate> <div class="<%# Container.ItemIndex == ((DataListObject)((RepeaterItem)Container.Parent.Parent) .DataItem).Count -1 ? "lastItem": "innerItem" %>"> </div> </ItemTemplate> </asp:Repeater> <ItemTemplate> </asp:Repeater> |
In this case, (RepeaterItem)Container.Parent.Parent does the trick.