Adding an empty data template to the repeater control

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 reference your new control (instead of the default one) and you're done.

Tags:
Overall Rating:
 
 [0 Vote(s)]

Bookmarks

de.lico.us Digg Facebook Reddit Stumble Upon
Added By: Lee Timmins on 26th Sep 2007 at 01:00
Last Updated: 02nd May 2009 at 12:55

Comments

There are currently no comments.

New Comment

You must be logged in to comment on this article. Please login or register.