Grouped Repeater .net

A neat solution to allow group headers on repeaters without using much code or nested repeaters. Rick Strahl provides a good example of this stragegy in C# i have modifid this slightly and converted to VB.

  <asp:Repeater ID="RPContent" runat="server">
                  <ItemTemplate>
                       <%# Me.RenderGroup(Eval("theyear")) %>
                     <div class="headline">
                    <h3><a href="<%# Eval("url  ")%>"><%# Eval("description") %></a></h3>
                       <p><%# Eval("thedatestr")%></p>
                    </div>       
                  </ItemTemplate>
  			</asp:Repeater>

Whilst the last closing div may look out of place it is required to close the last repeater item. the eval RenderGroup function passes the vasriable that you wish to group the repeater by. This function then returns a header div if the item in the repeater is part a new group or nothing if the next item in the repeater matches the same group as the preivous one.

I’ve added the active class to the div for the first group to be selected automaticaly with the jquery paging plugin this then pages the repeater list based on the group.

 Public LastGroup As String = &quot;@#@~&quot;

    Public Function RenderGroup(Group As String) As String
        If (Group = Me.LastGroup) Then
            Return &quot;&quot;
        Else
            '// *** Group has changed
            If LastGroup = &quot;@#@~&quot; Then
                'start group
                Me.LastGroup = Group
                Return &quot;&lt;div id='pane&quot; + Group + &quot;' class='tab-pane active'&gt;&quot;
            Else

                Me.LastGroup = Group
                Return &quot;&lt;/div&gt;&lt;div id='pane&quot; + Group + &quot;' class='tab-pane'&gt;&quot;
            End If
        End If
    End Function


Thanks to Rick for his orignal work.
http://www.west-wind.com/weblog/posts/2007/Aug/23/Creating-a-scrollable-and-grouped-Repeater-Layout

Leave a Reply

Your email address will not be published. Required fields are marked *