結合NBearDataSource實現嵌套Repeater

來源:互聯網
上載者:User
參考資源:
1. 在Repeater中嵌套使用Repeater
2. How To Display Hierarchical Data by Using Nested Repeater Controls and Visual C# .NET
3. 嵌套的Repeater
4. 在嵌套的repeater中加ItemDataBound事件
5. [CN.Text開發筆記]嵌套Repeater的問題
6. repeater中嵌套repeater(vs2005)
7. Repeater嵌套的兩種方案
8. Scott Mitchell 的ASP.NET 2.0資料教程之三十二:: 資料控制項的嵌套

最組:

方案一:每次綁定產生NBearDataSource,在ItemDataBound中設定NBearDataSource.Filter().
代碼如下:設計代碼
<%@ Register TagPrefix="nb" Namespace="NBear.Web.Data" Assembly="NBear.Web.Data" %>
<nb:NBearDataSource ID="nbdsLinkCategorys" runat="server" ConnectionStringName="Access" 
        TypeName="Entities.LinkCategory"></nb:NBearDataSource>
    <asp:Repeater ID="rptLinkCategories" DataSourceID="nbdsLinkCategorys" runat="server" OnItemDataBound="rptLinkCategories_ItemDataBound">
        <ItemTemplate>
        <div class="box">
        <h3><%#Eval("Name") %></h3>
        <ul><nb:NBearDataSource ID="nbdsLinks" runat="server" ConnectionStringName="Access"
        TypeName="Entities.Link" />
            <asp:Repeater ID="rptLinks" DataSourceID="nbdsLinks" runat="server">
                <ItemTemplate>
                <li>
                    <a href='<%#Eval("Src") %>' 
                            runat="server" title='<%#"點擊查看 - " + Eval("Title") %>'><%#Eval("Title") %></a>
                </li>
                </ItemTemplate>
            </asp:Repeater>
        </ul>
        </div>
        </ItemTemplate>
    </asp:Repeater>

原始碼
protected void rptLinkCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            LinkCategory category = (LinkCategory)e.Item.DataItem;
            NBearDataSource nbds = (NBearDataSource)e.Item.FindControl("nbdsLinks");
            nbds.Filter(Link._.CategoryID == category.ID);
        }
    }

缺點:如果有N個Linkcategory,這個方法會訪問資料庫N+1次— 一次返回所有的Linkcategory,N次返回特定Linkcategory下的Links。

方案二:只需兩次資料訪問,一次擷取全部的LinkCategories,一次擷取全部的Links。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.