Repeated Headers in a Repeater

R

Robert Walter

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++;
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top