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

后端 未结 5 1577
长情又很酷
长情又很酷 2021-02-09 07:19

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         


        
5条回答
  •  清酒与你
    2021-02-09 07:46

    [ToolboxData("<{0}:SmartRepeater runat=\"server\">")]
    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:

    
        HEADER
        Item
        , 
        Nothing
        FOOTER
    
    

提交回复
热议问题