Click here to Skip to main content
11,789,706 members (60,137 online)
Click here to Skip to main content

A Grouping Repeater Control for ASP.NET

, 16 Sep 2004 248.6K 4.4K 51
Rate this:
Please Sign up or sign in to vote.
This custom repeater control can be used to add group headers to your output.


Have you ever wondered why ASP.NET did not come with a repeater control that can group results? Well, I did and I decided to try to write one myself.

I have seen other solutions to this problem, but not as simple and elegant as this one, and that is why I decided to share it.


The Repeater control that comes with ASP.NET has one very cool event: the ItemCreated event. This event is fired whenever an element (or an instance of a template) is added to the Controls collection of the control.

Furthermore, the source demos in the ASP.NET SDK showed me how to use templates and data binding. Because it is documented in a lot of places on the web, I'm not going to repeat that here. Just hit Google with 'Templated Databound Control ASP.NET' and you'll see enough results.

The third part of the solution included the ability to provide a custom Comparer delegate that will compare 2 items of an unknown type.

The solution

What I did was the following:

  • Create a subclass of the System.Web.UI.WebControls.Repeater class.
  • Trap the ItemCreated event.
  • Add a GroupTemplate template to the control (in addition to the ItemTemplate).
  • For each item created, compare it to the previous item using a custom Comparer.
  • If it is different, instantiate the template and databind it using the same ItemData as the current Item.
  • Override the CreateChildControls method to reset the 'last record' member.

Anyway, I think this is a very good example of subclassing ASP.NET controls and I'm still wondering why this one isn't included in the standard framework.

Here's the code. It is also included in the demo solution file (see above).

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.ComponentModel;

namespace GroupedRepeater.Controls
 /// <summary>
 /// Summary description for GroupingRepeater.
 /// When another group is found, render an additional template.
 /// </summary>
 public class GroupingRepeater : System.Web.UI.WebControls.Repeater
  private ITemplate _groupTemplate = null;
  private IComparer _comparer = null;
  private static object lastvalue = null;

  public GroupingRepeater()
   this.ItemCreated += new 

  protected override void AddParsedSubObject(object obj)
   base.AddParsedSubObject (obj);

  public IComparer Comparer
   get { return _comparer; }
   set { _comparer = value; }

  public ITemplate GroupTemplate
    return _groupTemplate; 
    _groupTemplate = value; 

  protected override void CreateChildControls()
   lastvalue = null;
   base.CreateChildControls ();

  private void GroupingRepeater_ItemCreated(object sender, 
                                      RepeaterItemEventArgs e)
   if(e.Item.ItemType == ListItemType.Item || 
        e.Item.ItemType == ListItemType.AlternatingItem)
    if(e.Item.DataItem != null)
     if(_comparer.Compare(lastvalue, e.Item.DataItem) != 0)
      //add a header if it was different from the previous item.

      GroupHeader item = new GroupHeader();

      item.DataItem = e.Item.DataItem;

    lastvalue = e.Item.DataItem;

  public class GroupHeader : Control,INamingContainer
   private object _dataItem;

   public virtual object DataItem 
     return _dataItem;
     _dataItem = value;

That's it! I hope you find this useful.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Rob van der Veer
Web Developer
Netherlands Netherlands
Rob has been a professional web developer since 1998, and is working with C# and ASP.Net since early 2002.

Most of the time, his focus is on creating a clean and simple solution to development problems. (Although that sometimes means that he needs to do some hard work to make somebody else's life more easier.).

You may also be interested in...

Comments and Discussions

GeneralDifference in code sample / page code Pin
ssdjspik21-Jul-05 4:16
memberssdjspik21-Jul-05 4:16 
GeneralRe: Difference in code sample / page code Pin
uy_do19-May-06 4:35
memberuy_do19-May-06 4:35 
GeneralHelp needed rtying to make this work Pin
ROCNDAVE1-Jun-05 14:22
memberROCNDAVE1-Jun-05 14:22 
GeneralDLL Pin
gvkfaskljbvd18-May-05 10:16
membergvkfaskljbvd18-May-05 10:16 
GeneralHaving Trouble Pin
dhainswo15-May-05 9:23
memberdhainswo15-May-05 9:23 
GeneralItems.Count Pin
csstiffl20-Aug-04 11:31
membercsstiffl20-Aug-04 11:31 
GeneralRe: Items.Count Pin
Rob van der Veer21-Aug-04 0:40
memberRob van der Veer21-Aug-04 0:40 
GeneralRe: Items.Count Pin
Rob van der Veer16-Sep-04 23:02
memberRob van der Veer16-Sep-04 23:02 
GeneralPostback converts GroupHeader to RepeaterItem Pin
gninneh25-Jun-04 11:19
sussgninneh25-Jun-04 11:19 
GeneralRe: Postback converts GroupHeader to RepeaterItem Pin
Rob van der Veer12-Sep-04 11:45
memberRob van der Veer12-Sep-04 11:45 
GeneralRe: Postback converts GroupHeader to RepeaterItem Pin
Rob van der Veer16-Sep-04 23:00
memberRob van der Veer16-Sep-04 23:00 
Generalitemcommand problem Pin
joecod1-Apr-04 8:33
memberjoecod1-Apr-04 8:33 
GeneralRe: itemcommand problem Pin
Rob van der Veer2-Apr-04 6:20
memberRob van der Veer2-Apr-04 6:20 
GeneralRe: itemcommand problem Pin
joecod4-Apr-04 22:36
memberjoecod4-Apr-04 22:36 
GeneralRe: itemcommand problem Pin
Rob van der Veer3-May-04 11:19
memberRob van der Veer3-May-04 11:19 
GeneralRe: itemcommand problem Pin
Anonymous3-May-04 8:06
sussAnonymous3-May-04 8:06 
GeneralRe: itemcommand problem Pin
Rob van der Veer3-May-04 11:17
memberRob van der Veer3-May-04 11:17 
GeneralRe: itemcommand problem Pin
Rob van der Veer12-Sep-04 11:44
memberRob van der Veer12-Sep-04 11:44 
GeneralRe: itemcommand problem Pin
Anonymous15-Sep-04 18:29
sussAnonymous15-Sep-04 18:29 
GeneralRe: itemcommand problem Pin
Rob van der Veer16-Sep-04 23:01
memberRob van der Veer16-Sep-04 23:01 
QuestionRe: itemcommand problem Pin
debbie_g21-Mar-07 7:07
memberdebbie_g21-Mar-07 7:07 
AnswerRe: itemcommand problem Pin
Rob van der Veer3-May-07 11:20
memberRob van der Veer3-May-07 11:20 
GeneralNice work, couple suggestions ... Pin
Justin Bailey24-Feb-04 13:19
sussJustin Bailey24-Feb-04 13:19 
GeneralRe: Nice work, couple suggestions ... Pin
Rob van der Veer3-May-04 11:18
memberRob van der Veer3-May-04 11:18 
GeneralSnapshot Is Needed Pin
<marquee>Fayez Mutairi</marquee>22-Feb-04 20:20
memberFayez Mutairi22-Feb-04 20:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1509028.1 | Last Updated 16 Sep 2004
Article Copyright 2004 by Rob van der Veer
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid