web 2.0

How-To Handle Empty Data With ASP.NET Repeater

Hi,

In this article I'll provide a simple trick in order to solve the missing ASP.NET Repeater Empty Data Template ...

By Default ASP.NET Repeater provides 4 Builtin Templates

  • HeaderTemplate
  • ItemTemplate
  • AlternateItemTemplate
  • FooterTemplate

We often need to show something when there is no data .... For example show "No Data" Message !

<EmptyTemplate>
    <strong>No Data !</strong>
</EmptyTemplate>

So there is the trick, Create a Custom Server Control inherited from System.Web.UI.WebControls.Repeater as the following

[ToolboxData("<{0}:CustomRepeater runat=server></{0}:CustomRepeater>")]
public class CustomRepeater : Repeater
{
    [Category("Data")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate EmptyTemplate
    {
        get;
        set;
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);

        if (this.Items.Count <= 0 && EmptyTemplate != null)
        {
            EmptyTemplate.InstantiateIn(this);
        }
    }
}

Now we got our EmptyTemplate but we can improve this a little bit more. I want to show HeaderTemplate and FooterTemplate even if there is no data.

So here is the modification to add to our Custom Repeater.

[ToolboxData("<{0}:CustomRepeater runat=server></{0}:CustomRepeater>")]
public class CustomRepeater : Repeater
{
    [Category("Data")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate EmptyTemplate
    {
        get;
        set;
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);

        if (this.Items.Count <= 0 && EmptyTemplate != null)
        {
            HeaderTemplate.InstantiateIn(this);

            EmptyTemplate.InstantiateIn(this);

            FooterTemplate.InstantiateIn(this);
        }
    }
}

 

UPDATE 13/12/2008

After some tests, this solution seems to not works when working with PostBack.

HeaderTemplate, EmptyDataTemplate and FooterTemplate are not re-created on PostBack. So we need to Overrides CreateChildControls methods.

Here is the Updated Code :

[ToolboxData("<{0}:CustomRepeater runat=server></{0}:CustomRepeater>")]
public class CustomRepeater : Repeater
{
    #region Public Properties

    [Category("Data")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate EmptyTemplate
    {
        get;
        set;
    }

    #endregion

    #region Protected Overrides Methods

    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        HandleEmptyData();
    }
    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);

        HandleEmptyData();
    }

    #endregion

    #region Private Methods

    private void HandleEmptyData()
    {
        if (this.Items.Count <= 0 && EmptyTemplate != null)
        {
            this.Controls.Clear();
            HeaderTemplate.InstantiateIn(this);
            EmptyTemplate.InstantiateIn(this);
            FooterTemplate.InstantiateIn(this);
        }
    }

    #endregion
}

 

Hope this help's!



Views(2271)

kick it on DotNetKicks.com

Share/Save/Bookmark Subscribe

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.NET

Comments

Technorati Profile