Referencing parent repeater items from server controls

This is something I’ve had to do a couple of times in the past, and someone else in work had the same requirement last week. The situation this resolve is that you’ve got a repeater on your page, but within this you have other controls which need to be aware of their context, for example to amend their behaviour according to which item is being rendered.

In this example, I wanted the last item in my unordered list to have a different look/feel to the other items, which was achieved using a couple of different placeholders and attaching a different style to the last item only. The “Visible” property of each placeholder is then set accordingly:

<asp:Repeater id=”myRepeater” Runat=”server”>
<asp:PlaceHolder ID=”normalItem” runat=”server” Visible='<%#Container.ItemIndex != ((ICollection)((Repeater)Container.Parent).DataSource).Count – 1%>’><li><%Eval(“SomeField”) %></li></asp:PlaceHolder>
<asp:PlaceHolder ID=”lastItem” runat=”server” Visible='<%#Container.ItemIndex == ((ICollection)((Repeater)Container.Parent).DataSource).Count – 1%>’><li class=”last”><%Eval(“SomeField”) %></li></asp:PlaceHolder>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: