Steve Kinsey.Com

subscribe to the RSS Feed

Thursday, July 29, 2010

Having More Than Two Sections In An ASP.Net Repeater Control

ASP.Net Repeater Control
If you’re familiar with the ASP.Net repeater control then you will know that when laying out the repeater contents you can have individual style for alternating rows so far example every other row could have a different background. But what if you wanted more sophisticated options, say for example you wanted to do a news style repeater with the first item having a photo and an in depth story, then next five having a picture and summary and the remaining items having just a headline.

Well I found a great tip on how you can do this with a single repeater. The way to do it is to set up panels inside your repeaters item template, as the example below:

<asp:Repeater id="myRepeater" runat="server">

<ItemTemplate>

<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# (Container.ItemIndex  = 0) %>' >
<div>

My First Item Controls

</div>
</asp:PlaceHolder>

<asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# (Container.ItemIndex >=1) and (Container.ItemIndex <5) %>' >
<div class="underlined">

My Next Five Item Controls

</div>
</asp:PlaceHolder>

<asp:PlaceHolder ID="PlaceHolder3" runat="server" Visible='<%# (Container.ItemIndex >= 5) %>' >
<div>

The Reminder Of My Items Controls

</div>
</asp:PlaceHolder>

</ItemTemplate>

</asp:Repeater>

So you set up a panel for each of the individual styles that you require and then the Visible property of the panel is derived from where we are currently in the list of items. Nice and easy, requires no complex code behind tricks and works like a charm.

Hope this will be of help to someone out there, it certainly was to me.

Bookmark and Share

Enjoy This Post?


Add A Comment

CommentLuv Enabled