如何隐藏在ASP.NET C#中的中继如果数据源不包含的项目? [英] How can I hide a repeater in ASP.NET C# if the DataSource contains no items?

查看:125
本文介绍了如何隐藏在ASP.NET C#中的中继如果数据源不包含的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用嵌套在另一个中继器内产生的数据的列表中继的ASP.NET页面。这是下面的效果:

 < ASP:直放站>
    <&ItemTemplate中GT;
        <跨度><%#的eval(数据1)%GT;< / SPAN>
        <! - 还有更多 - >
        < ASP:直放站的DataSource ='<%#的eval(数据2)%>'>
            <&HeaderTemplate中GT;
                < UL>
            < / HeaderTemplate中>
            <&ItemTemplate中GT;
                <立GT;<%#的Container.DataItem%GT;< /李>
            < / ItemTemplate中>
            < FooterTemplate>
                < / UL>
            < / FooterTemplate>
        < / ASP:直放站>
    < / ItemTemplate中>
< / ASP:直放站>

在(C#)code隐藏我基本上使用LINQ to从XML文档拉动信息的列表并绑定该信息的第一个中继器。

搜索的答案,似乎该方法是确定嵌套中继器中的数据是否为空。如果是,那么你中继器的可见性设置为false。

不幸的是,我一直没能确定该怎么做内联,而不是在code-背后(因为它并不一定是我在做什么工作)。

由于我的网页现在还没有确认,因为UL最终因为没有任何数据2项是空的,因为我想用一个无序列表保存,我寻求你们的帮助。

任何想法?

谢谢!

更新:

如果有帮助,因为它很可能是有可能在$ C $做C-的背后,则是LINQ的东西这样的效果:

  VAR X =从旋转Z Y
    选择新{
        数据1 = D,
        //诸如此类
        数据2 =(从j为K
            其中,j.Value!=的String.Empty
            选择j.Value).ToList()
    };blah.DataSource = X;
blah.DataBind();


解决方案

这不会的隐藏直放站完全,但你也可以继承Repeater控件,以便它包含一个GridView般空数据模板:

 使用系统;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;公共类EmptyCapableRepeater:直放站
{
    公开了Itemplate EmptyDataTemplate {搞定;组; }    保护覆盖无效OnDataBinding(EventArgs的发送)
    {
        base.OnDataBinding(E);        如果(this.Items.Count == 0)
        {
            EmptyDataTemplate.InstantiateIn(本);
        }
    }
}

您可以将它们使用在.aspx是这样的:

 <定制:EmptyCapableRepeater =服务器ID =rptSearchResults>
    <&ItemTemplate中GT;
        <%#的eval(结果)%GT;
    < / ItemTemplate中>
    < SeparatorTemplate>
        < BR />
    < / SeparatorTemplate>
    < EmptyDataTemplate>
        <环境监察及GT;没有找到< / EM>
    < / EmptyDataTemplate>
< /定制:EmptyCapableRepeater>

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: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.

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.

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.

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:

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();

解决方案

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>

这篇关于如何隐藏在ASP.NET C#中的中继如果数据源不包含的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆