Repeated Headers in a Repeater

From: Robert Walter (weeerob_at_hotmail.com)
Date: 04/26/04


Date: 26 Apr 2004 05:10:45 -0700

I was looking for a method to repeat the headers on an ASP.NET
Repeater control. I couldn't find quite what I wanted on the web so I
implemented it myself. I thought it maybe useful for some of you out
there (code is below). It works exactly the same as a normal repeater
except that there is an extra property HeaderRepeatRows in it.

Note, if you use this generate a table then put the <table> tag
outside the header template otherwise it'll be repeated each time the
header is.

A similar method can probably be used to do the same for a datagrid
but it's made a bit more difficult as you have the header
automatically being generated.

Rob.

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

namespace Pjv.Pjo.Controls
{
        /// <summary>
        /// Summary description for Repeater.
        /// </summary>
        public class Repeater : System.Web.UI.WebControls.Repeater
        {
                private int _headerRepeatRows = 0;
                private int _repeatedRows = 0;

                public int HeaderRepeatRows
                {
                        get
                        {
                                return _headerRepeatRows;
                        }
                        set
                        {
                                _headerRepeatRows = value;
                        }
                }

                public Repeater()
                {
                        this.ItemCreated += new
RepeaterItemEventHandler(GroupingRepeater_ItemCreated);
                }

                private void GroupingRepeater_ItemCreated(object sender,
RepeaterItemEventArgs e)
                {
                        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType ==
ListItemType.AlternatingItem))
                        {
                                if ((_headerRepeatRows > 0) && (_repeatedRows ==
_headerRepeatRows))
                                {
                                        RepeaterItem header = new RepeaterItem(0, ListItemType.Header);

                                        HeaderTemplate.InstantiateIn(header);

                                        this.Controls.Add(header);

                                        _repeatedRows = 0;
                                }

                                _repeatedRows++;
                        }
                }
        }
}