ASP.NET 2.0 Resources

Powered by Blogger

.NET 2.0 Repeater - Different template for first item

I've got just another little trick with repeater control for you. Have you ever needed more complex rules for rendering ASP.NET 2.0 Repeater items not just ItemTemplate and AlternatingItemTemplate templates? For example different style/layout for the first item?

There is one really simple solution how to declaratively achieve such behavior. Simply define multiple placeholders and show/hide them according to data Container's ItemIndex property. The following example shows how to check for first repeater item...

   1:  <asp:Repeater runat="server">
   2:      <ItemTemplate>
   3:          <asp:PlaceHolder runat="server" Visible="<%# (Container.ItemIndex == 0) %>" >
   4:          </asp:PlaceHolder>
   5:      </ItemTemplate>
   6:  </asp:Repeater>

7 Comments:

  • I realize this is well after you've posted this solution, but I faced a similar problem and after some tedious searching, including finding several suggestions that one should completely rewrite the Repeater control, which is a bit more work than I could justify spending time on, I found your post here, and I must say that this is a simple and brilliant solution to my problem, thanks for your post!!

    By Blogger Tim, at 12:07 AM  

  • Brilliant! I was unaware of this property.

    By Blogger David Garcia, at 4:35 AM  

  • Repeater doesn't actually support this property - the trick instead is to use two Placeholders within the ItemTemplate.

    The first has a

    Visible="<%# (Container.ItemIndex == 0) %>"

    property and the other has the opposite so that it doesn't show the first time, but does the rest

    Visible="<%# (Container.ItemIndex != 0) %>"

    By Blogger Judder, at 5:10 PM  

  • Genius.

    By Blogger robkroese, at 6:57 PM  

  • to surpass the needs of the aforementioned we serve and teach, and to team up with http://www.customwritings.ws others in our group to enhance the health status of youngsters.

    By Blogger ADmin, at 8:53 AM  

  • This comment has been removed by the author.

    By Blogger aamirafridi.com, at 3:59 PM  

  • What about last item?
    I want to add a css class to the last li

    By Blogger aamirafridi.com, at 3:59 PM  

Post a Comment

<< Home

Created dolly