如果数据源不包含任何项目,如何在 ASP.NET C# 中隐藏转发器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/327151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:20:57  来源:igfitidea点击:

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

c#asp.netrepeater

提问by James Skemp

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:

我有一个 ASP.NET 页面,它使用嵌套在另一个转发器中的转发器来生成数据列表。它的作用如下:

<asp:Repeater>
    <ItemTemplate>
        <span><%#Eval("Data1") %></span>
        <!-- and many more -->
        <asp:Repeater DataSource='<%#Eval("Data2")%>'>
            <HeaderTemplate>
                <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li><%#Container.DataItem%></li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>
            </FooterTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

In the (C#) code-behind I'm basically using LINQ to pull a listing of information from an XML document and bind that information to the first repeater.

在 (C#) 代码隐藏中,我基本上使用 LINQ 从 XML 文档中提取信息列表并将该信息绑定到第一个转发器。

Searching for the answer to this, it seems the method is to determine whether the data for the nested repeater is empty. If it is, then you set the visibility of the repeater to false.

寻找这个问题的答案,似乎方法是确定嵌套转发器的数据是否为空。如果是,则将转发器的可见性设置为 false。

Unfortunately, I haven't been able to determine how to do that inline, and not in the code-behind (since it won't necessarily work for what I'm doing).

不幸的是,我无法确定如何内联执行该操作,而不是在代码隐藏中(因为它不一定适用于我正在做的事情)。

Since my pages aren't validating now, because the ul ends up being empty for any items without Data2, and because I'd like to keep using an unordered list, I seek your help.

由于我的页面现在没有验证,因为对于没有 Data2 的任何项目,ul 最终为空,并且因为我想继续使用无序列表,所以我寻求您的帮助。

Any ideas?

有任何想法吗?

Thanks!

谢谢!

UPDATE:

更新:

If it helps, since it could very well be possible to do in the code-behind, the LINQ is something to this effect:

如果它有帮助,因为它很可能在代码隐藏中完成,LINQ 就是这种效果:

var x = from y in z
    select new {
        Data1 = d,
        // etcetera
        Data2 = (from j in k
            where j.Value != String.Empty
            select j.Value).ToList()
    };

blah.DataSource = x;
blah.DataBind();

采纳答案by Matt Peterson

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

这不会完全隐藏转发器,但您可以将转发器控件子类化,以便它包含一个类似于 GridView 的空数据模板:

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:

你可以像这样在你的 .aspx 中使用它:

<custom:EmptyCapableRepeater runat="server" ID="rptSearchResults">
    <ItemTemplate>
        <%# Eval( "Result" )%>
    </ItemTemplate>
    <SeparatorTemplate>
        <br />
    </SeparatorTemplate>
    <EmptyDataTemplate>
        <em>No results were found.</em>
    </EmptyDataTemplate>
</custom:EmptyCapableRepeater>

回答by James Skemp

When you get your LINQ query executed, check its Count property (providing its a list of some sort). If its 0, then just turn the Visible property to false.

当您执行 LINQ 查询时,检查其 Count 属性(提供某种列表)。如果为 0,则只需将 Visible 属性设置为 false。

回答by Mitchel Sellers

As far as I know you must do this via the codebehind, just use the ItemDataBound event to handle it, you can leave pretty much everything as is, just simply input some logic that gets the dataset and determines if it has entries, if not hide the repeater.

据我所知,您必须通过代码隐藏来执行此操作,只需使用 ItemDataBound 事件来处理它,您几乎可以保留所有内容,只需输入一些获取数据集并确定它是否有条目的逻辑,如果没有隐藏中继器。

回答by Aleris

Try something like:

尝试类似:

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

for the nested repeater

对于嵌套中继器

回答by JoshBerke

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:

我不认为您正在做的事情会起作用,当我尝试按照您的方式设置 DataSource 时出现错误;然而,在你背后的代码中这样做:

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.

假设您向父中继器的 ItemDataBoundEvent 添加了一个侦听器,那么您需要稍微更改 linq 查询以不使用匿名类型(创建具有您的属性的受保护类)在 mjy 情况下,我使用 dto 作为类名。

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.

我很想知道为什么你认为你不能在背后的代码中解决这个问题。

回答by Michael Silver

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

为什么不使用 ListView?它提供了许多相同的功能,包括 EmptyDataTemplate。

回答by Michael Silver

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!

希望对某人有所帮助!

回答by Brian

In the OnItemDataBoundevent, set visibility to false if ItemTypeis a Headerand set visibility to true if ItemTypeis an Item.

OnItemDataBound事件中,设置可见假,如果ItemType是一个Header和集可视性为true,如果ItemType是一个Item

回答by Reza

use this:

用这个:

protected void Repeater1_PreRender(object sender, EventArgs e)
{
    if (Repeater1.Items.Count < 1)
    {
        container.Visible = false;
    }
}