Extends Repeater control's EmptyDataTemplate template functionality

  • 2020-05-19 04:40:07
  • OfStack

The Repeater control is a data display control that allows you to customize the layout by repeating the specified template for each item displayed in the list.

Compared to GridViews and DataList, Repeater is a fairly lightweight, flexible control that consumes much less energy. The fly in the ointment is that the functionality is a little thin, especially if the data source used for binding has no data, often using a hidden version to display "no data at all" information. It's too much trouble.

The EmptyDataTemplate template, which extends the Repeater control itself, has achieved the same effect.

(vs2008) customize an Repeater control with the functional solution of EmptyDataTemplate templates - "add -" new project - "select C# class library
Add a reference to the newly created class library in the.NET TAB and select System.Web
 
/// <summary> 
///  The custom Repeater support EmptyDataTemplate 
///  The author :cantops 
/// </summary> 
public class Repeater :System.Web.UI.WebControls.Repeater 
{ 
private ITemplate emptyDataTemplate; 

[PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(TemplateControl))] 
public ITemplate EmptyDataTemplate 
{ 
get { return emptyDataTemplate; } 
set { emptyDataTemplate = value; } 
} 
protected override void OnDataBinding(EventArgs e) 
{ 

base.OnDataBinding(e); 
if (emptyDataTemplate != null) 
{ 
if (this.Items.Count == 0) 
{ 
EmptyDataTemplate.InstantiateIn(this); 
} 
} 
} 

} 

Then make a direct reference to the user control used.

Related articles: