In ASP.NET we have repeater control which is cool...!!, last week I was developing a website in which I want the content to be grouped alphabetically. For example if we have a list of countries, I want it to be grouped alphabetically with corresponding alphabet on the top the group. But our old friend repeater control couldn't meet this requirement. So I thought about creating a custom repeater controls, I call this ExRepeater(Extended Repeater), this control helps you to group the items in the repeater according to any fields you specify, more over the Grouping Logic can be decided by the user. That is the control doesn't provide any grouping logic from out-of-box.
So let starts creating this...
I have create a control library project with the name PlugAI.Controls.Web.UI.ExRepeater and added a class called ExRepeater which is derived from the CompositeDataBoundControl
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Collections;
using System.Drawing.Design;
using System.Data;
using System.ComponentModel;
[assembly: TagPrefix("PlugAI.Controls.Web.UI.ExRepeater", "PlugAI")]
namespace PlugAI.Controls.Web.UI.ExRepeater
{
public class ExRepeater : CompositeDataBoundControl
{
.....
}
}
After creating this class, you have override the CreateChildControls(....) method, this method is responsible for generating the output we want.
protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
{
int count = 0;
if (dataBinding)
{
........
...........
foreach (object dataItem in dataSource)
{
if (itemTemplate != null)
{
....................
ExRepeaterItem item = new ExRepeaterItem(count++,ExListItemType.Item);
item.DataItem = dataItem;
ItemTemplate.InstantiateIn(item);
................
this.Controls.Add(item);
item.DataBind();
...............
}
}
................
}
return count;
}
Here first we check whether we want the data binding, if required we will take each data item from the binded data source and create corresponding controls from the template.
In the next post I will explain about
- Raising events, like ItemCommand etc..
- Creating a templated control
- Developing the Grouping Logic
- Creating designer for the Repeater Control
- Finally a sample webpage with ExRepeater control.




