Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a custom Repeater that displays Header, Footer based on properties?

I want to create a Repeater that displays the header/footer based on properties, only if the DataSource is empty.

public class Repeater : System.Web.UI.WebControls.Repeater
{
    public bool ShowHeaderOnEmpty { get; set; }
    public bool ShowFooterOnEmpty { get; set; }

    [DefaultValue((string)null),
    PersistenceMode(PersistenceMode.InnerProperty),
    TemplateContainer(typeof(System.Web.UI.WebControls.RepeaterItem)),
    Browsable(false)]
    public ITemplate EmptyTemplate { get; set; }
}

I also want to create a EmptyTemplate, if the DataSource is empty display this template...

I have no idea on how to implement this. What should I override to achieve this behavior?

like image 846
BrunoLM Avatar asked Sep 17 '10 14:09

BrunoLM


2 Answers

[ToolboxData("<{0}:SmartRepeater runat=\"server\"></{0}:SmartRepeater>")]
public partial class SmartRepeater : Repeater
{
    public bool ShowHeaderOnEmpty { get; set; }
    public bool ShowFooterOnEmpty { get; set; }

    private ITemplate emptyTemplate = null;

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate EmptyTemplate
    {
        get { return this.emptyTemplate; }
        set { this.emptyTemplate = value; }
    }

    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);
        if (this.Items.Count == 0)
        {
            this.Controls.Clear();

            if (this.HeaderTemplate != null && ShowHeaderOnEmpty)
                this.HeaderTemplate.InstantiateIn(this);

            if (this.EmptyTemplate!=null)
                this.EmptyTemplate.InstantiateIn(this);

            if (this.FooterTemplate != null && ShowFooterOnEmpty)
                this.FooterTemplate.InstantiateIn(this);
        }
    }
}

Usage:

<UC:SmartRepeater ID="rep" runat="server" ShowHeaderOnEmpty="true" ShowFooterOnEmpty="true">
    <HeaderTemplate>HEADER</HeaderTemplate>
    <ItemTemplate>Item</ItemTemplate>
    <SeparatorTemplate>, </SeparatorTemplate>
    <EmptyTemplate><b>Nothing</b></EmptyTemplate>
    <FooterTemplate>FOOTER</FooterTemplate>
</UC:SmartRepeater>
like image 163
Pavlo Neiman Avatar answered Sep 24 '22 21:09

Pavlo Neiman


Use ListView instead of Repeater. It already contains EmptyDataTemplate and EmptyItemTemplate elements so you don't need to do anything :)

like image 25
Alexey Raga Avatar answered Sep 22 '22 21:09

Alexey Raga