How can I hide a repeater in ASP.NET C# if the DataSource contains no items?

前端 未结 9 1913
小鲜肉
小鲜肉 2021-01-02 17:29

I have an ASP.NET page that uses a repeater nested within another repeater to generate a listing of data. It\'s to the effect of the following:



        
9条回答
  •  耶瑟儿~
    2021-01-02 18:11

    This won't hide the repeater completely, but you can subclass the Repeater control so that it includes a GridView-like empty data template:

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public class EmptyCapableRepeater : Repeater
    {
        public ITemplate EmptyDataTemplate { get; set; }
    
        protected override void OnDataBinding ( EventArgs e )
        {
            base.OnDataBinding( e );
    
            if ( this.Items.Count == 0 )
            {
                EmptyDataTemplate.InstantiateIn( this );
            }
        }
    }
    

    You can them use it in your .aspx like this:

    
        
            <%# Eval( "Result" )%>
        
        
            
    No results were found.

提交回复
热议问题