簡體   English   中英

如果DataSource不包含任何項目,如何在ASP.NET C#中隱藏轉發器?

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

我有一個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>

在(C#)代碼隱藏中,我基本上使用LINQ從XML文檔中提取信息列表並將該信息綁定到第一個轉發器。

尋找答案,似乎方法是確定嵌套轉發器的數據是否為空。 如果是,則將轉發器的可見性設置為false。

不幸的是,我無法確定如何內聯,而不是在代碼隱藏中(因為它不一定適用於我正在做的事情)。

由於我的頁面現在沒有驗證,因為對於沒有Data2的任何項目,ul最終為空,並且因為我想繼續使用無序列表,我尋求你的幫助。

有任何想法嗎?

謝謝!

更新:

如果它有幫助,因為它很可能在代碼隱藏中完成,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();

這不會完全隱藏轉發器,但您可以繼承Repeater控件,使其包含類似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 );
        }
    }
}

您可以在.aspx中使用它,如下所示:

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

嘗試類似的東西:

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

對於嵌套的中繼器

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

用這個:

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

我知道這是一個老線程,上面的答案是一個非常好的解決方案,但我有一個類似的問題,並找到了另一個簡單的解決方案,我想我也會分享。 這樣可以很好地驗證並顯示相同的內容。

只需將頁腳模板更改為:

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

或者如果您使用表格:

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

希望有人幫助!

OnItemDataBound事件中,如果ItemTypeHeader ,則將可見性設置為false,如果ItemTypeItem ,則將可見性設置為true。

當您執行LINQ查詢時,請檢查其Count屬性(提供某種類型的列表)。 如果為0,則只需將Visible屬性設置為false。

據我所知,你必須通過代碼隱藏來做到這一點,只需使用ItemDataBound事件來處理它,你可以保留幾乎所有的東西,只需輸入一些獲取數據集的邏輯並確定它是否有條目,如果不隱藏轉發器。

我不認為你在做什么會起作用我在嘗試設置DataSource時會遇到錯誤; 但是,在你背后的代碼中你這樣做:

假設您向父轉發器的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();
}

我很想知道為什么你認為你無法在后面的代碼中解決這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM