Adding an empty data template to the repeater control
Wednesday, September 26th, 2007Hi, i was looking around for an article on how to extend the repeater control to add an empty data template similar to the GridView control. Here is a link to the the article i found. However i discovered that it rendered the header and footer, so i set about creating my own one and come up with the following:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ControlLibrary
{
public class Repeater : System.Web.UI.WebControls.Repeater
{
private ITemplate _emptyDataTemplate;[Browsable(false)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate EmptyDataTemplate
{
get { return _emptyDataTemplate; }
set { _emptyDataTemplate = value; }
}protected override void Render(HtmlTextWriter output)
{
// If there is no data then we don't wish to render anything (including the header and the footer)
if (this.Items.Count == 0)
{
// If an empty data template has been provided then display it
if (this.EmptyDataTemplate != null)
{
PlaceHolder phdTemplate = new PlaceHolder();
this.EmptyDataTemplate.InstantiateIn(phdTemplate);
phdTemplate.RenderControl(output);
}
}
else
base.Render(output);
}
}
}
Now all you have to do is say:
<%@ Register TagPrefix=”Flixon” Namespace=”ControlLibrary” Assembly=”ControlLibrary” %>
<Flixon:Repeater>
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
<EmptyDataTemplate>
No data returned!
</EmptyDataTemplate>
</Flixon:Repeater>
on your page and you’re done.
I hope this helps.