Search code examples
asp.netrepeater

ASP.Net Repeater Header Per Group (i.e. Month)


I have a datasource which contains dated items per row. These will be bound to the repeater and ordered by date. I'd like to present each month as a seperate table when rendered but is there a way to do this with a repeater control without having to have multiple repeaters added dynamically from the server side code?

Ideally I'd like the following:

Examples Data:

Row 1: Title 1, 01/12/2009
Row 2: Title 1, 02/12/2009
Row 1: Title 1, 01/01/2010
Row 1: Title 1, 02/01/2009

Required output:

Dec 09
-------------------------------
Title  |  Date                 |
-------------------------------|
Title1 |  01/12/2009           |
-------------------------------|
Title2 |  02/12/2010           |
-------------------------------|

Jan 10
-------------------------------
Title  |  Date                 |
-------------------------------|
Title1 |  01/01/2010           |
-------------------------------|
Title2 |  02/01/2010           |
-------------------------------|

Solution

  • You can use PlaceHolder controls within the ItemTemplate to contain the repeating goup headers and footers. Then set the visibility of the PlaceHolders in the OnItemDataBound or OnItemCreated event handlers based on the dates. Like this:

    <ItemTemplate>
    
    <asp:PlaceHolder id="table_end" visible="<%# _newMonthStarting && (!_firstMonth) %>" runat="server">
      </table>
    </asp:PlaceHolder>
    
    <asp:PlaceHolder id="table_end" visible="<%# _newMonthStarting %>" runat="server">
      <div><%# _monthHeader</div>
      <table>
        <tr>
          <th>Title</th>      
          <th>Date</th>      
        </tr>
    </asp:PlaceHolder>
      <tr>
        <td><!-- data --></td>
        <td><!-- data --></td>
      </tr>
    </ItemTemplate>
    
    <FooterTemplate>
       </table>
    </FooterTemplate>