May
30
2007
Repeater control and separator
Posted by admin under
Controls
This is kind of a twist of the article Insert user control every N records in gridview . I am gonna show you (and give you the C# code) for a ASP.NET 2.0 repeater control giving you some flexibility with regards to repeater items. This technique is totally different, in the previous article we (despite the obvious fact that we used a gridview instead of a repeater) modified the underlying datasource for the control - here we are gonna work with the <SeparatorTemplate>.
<asp:Repeater id="repe" runat="server" OnItemDataBound="repe_ItemDataBound" >
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><asp:Literal ID="litNamn" runat="server"></asp:Literal></li>
</ItemTemplate>
<SeparatorTemplate>
<li><b>This is a separator</b></li>
</SeparatorTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
Considering a repeater defined as above if would render something like this:

Now I have created a control - ASPCodeRepeatControl.RepeatControl which lets you specify two extra properties for the repeater
- SeparateAfter
- MaxSeparations
SeparateAfter lets you specify after how many items the separator should be shown. If you look in the code it simply Clear the SeparatorItem if enough items has not been rendered. So this type of definition
<aspcode:RepeatControl id="repe" runat="server"
OnItemDataBound="repe_ItemDataBound"
SeparateAfter="2" >
gives us this result:

The MaxSeparation property allows you to specify maximum number of separators to show:
<aspcode:RepeatControl id="repe" runat="server"
OnItemDataBound="repe_ItemDataBound"
SeparateAfter="1" MaxSeparations="2" >
gives you this:

In the download (C#, ASP.NET 2.0) you will get a web project - please modify the connectionstring in default.aspx (using Northwind) and also the control project.
The actual control is really so simple, here's all the code:
public class RepeatControl : Repeater
{
private int m_nCountRecords = 0;
private int m_nCountSeparators = 0;
public int SeparateAfter
{
get
{
if (ViewState["SeparateAfter"] == null)
return 1;
return Convert.ToInt32(ViewState["SeparateAfter"]);
}
set
{
ViewState["SeparateAfter"] = value;
}
}
public int MaxSeparations
{
get
{
if (ViewState["MaxSeparations"] == null)
return -1;
return Convert.ToInt32(ViewState["MaxSeparations"]);
}
set
{
ViewState["MaxSeparations"] = value;
}
}
protected override void OnInit(EventArgs e)
{
this.ItemDataBound += new RepeaterItemEventHandler(Class1_ItemDataBound);
base.OnInit(e);
}
void Class1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
m_nCountRecords = 0;
m_nCountSeparators = 0;
}
if (e.Item.ItemType == ListItemType.Separator)
{
bool fShowSeparator = false;
m_nCountRecords++;
if (m_nCountRecords == SeparateAfter && (MaxSeparations == -1 || m_nCountSeparators < MaxSeparations))
fShowSeparator = true;
if ( m_nCountRecords == SeparateAfter )
m_nCountRecords = 0;
if (fShowSeparator)
{
m_nCountSeparators++;
}
else
{
e.Item.Controls.Clear();
}
}
}
}
As you can see, no magicat all. Just some counters keeping track on how many regular items and separators has been shown, and - if we are NOT supposed to show the separator we just remove all controls inside it
e.Item.Controls.Clear();