Adding an empty data template to the repeater control
Hi, 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.
October 26th, 2007 at 2:08 pm
EmptyRepeater - Repeater with Empty Template…
…
October 30th, 2007 at 2:31 am
Hi, I found my solution above does not allow me to place controls within the empty data template, therefore i eventually went for:
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 OnItemCreated(RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer && this.Items.Count == 0)
{
// If an empty data template has been provided then display it
if (this.EmptyDataTemplate != null)
{
// Hide the header
this.Controls.Clear();
// Display the empty data template
Control templateContainer = new Control();
this.EmptyDataTemplate.InstantiateIn(templateContainer);
this.Controls.Add(templateContainer);
// Now hide the footer
e.Item.Visible = false;
}
}
else
base.OnItemCreated(e);
}
}
It does mean that your repeater has to have a footertemplate.