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

前端 未结 9 1839
小鲜肉
小鲜肉 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:04

    I don't think what you are doing is going to work I get an error when I try and set the DataSource as you are trying to do; however, in the code behind you do this:

    Assuming you added a listener to your parent repeater's ItemDataBoundEvent, then you will need to change your linq query slightly to not use an anonymous type (Create a protected class that has your properties) In mjy case I am using dto as the class name.

    void rep1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    
        Repeater rep2 = (Repeater)e.Item.FindControl("rep2");
        rep2.DataSource = ((dto)e.Item.DataItem).y;
        rep2.DataBind();
    }
    

    I'd love to learn why you think you can't solve this in the code behind.

    0 讨论(0)
  • 2021-01-02 18:05

    use this:

    protected void Repeater1_PreRender(object sender, EventArgs e)
    {
        if (Repeater1.Items.Count < 1)
        {
            container.Visible = false;
        }
    }
    
    0 讨论(0)
  • 2021-01-02 18:07

    Try something like:

    <asp:Repeater runat="server" DataSource='<%#Eval("Data2")%>' 
        Visible='<%# ((IEnumerable)Eval("Data2")).GetEnumerator().MoveNext() %>'>
    

    for the nested repeater

    0 讨论(0)
  • 2021-01-02 18:10

    Why not use a ListView? It offers much of the same functionality including an EmptyDataTemplate.

    0 讨论(0)
  • 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:

    <custom:EmptyCapableRepeater runat="server" ID="rptSearchResults">
        <ItemTemplate>
            <%# Eval( "Result" )%>
        </ItemTemplate>
        <SeparatorTemplate>
            <br />
        </SeparatorTemplate>
        <EmptyDataTemplate>
            <em>No results were found.</em>
        </EmptyDataTemplate>
    </custom:EmptyCapableRepeater>
    
    0 讨论(0)
  • 2021-01-02 18:13

    I know this is an old thread and the answer above is a very nice solution, but I had a similar problem and have found another vary simple solution I thought I would share also. This validates just fine and displays the same.

    Just change your footer template to:

    <FooterTemplate> 
                <li style="display:none;">This will not show.</li></ul> 
    </FooterTemplate> 
    

    Or if your using tables:

    <FooterTemplate> 
                <tr> style="display:none;"><td>But something must be in here.</td></tr></table> 
    </FooterTemplate> 
    

    Hope that helps someone!

    0 讨论(0)
提交回复
热议问题