简体   繁体   English

在嵌套中继器中显示层次结构数据

[英]Displaying Hierarchical Data in Nested Repeaters

I have a entity in my database that looks like this; 我的数据库中有一个看起来像这样的实体;

Item
{
    ID,
    Text,
    Description,
    ...
    ParentID
}

Where ParentID references another Item.ID; 其中ParentID引用另一个Item.ID;

I want to list these Items hierarchically using ASP.net. 我想使用ASP.net逐级列出这些项目。 I assume I want to use a asp:repeater to do the nesting, but maybe not. 我假设我想使用asp:repeater进行嵌套,但也许不是。 If there's a better option I'm all for it. 如果有更好的选择,我全力以赴。 I'm specifically interested in 3+ levels of nesting. 我对3层以上的嵌套特别感兴趣。

I'm using linq C# 4.0 if that matters. 如果有的话,我正在使用linq C#4.0。

TreeView might be a good choice for displaying hierarchical data. TreeView可能是显示分层数据的不错选择。 You can find lots of example for binding treeview with linq entities. 您可以找到许多将linq实体绑定到树视图的示例。 However Repeater is still a choice. 但是,Repeater仍然是一个选择。

The article below will be helpful about XML, LINQ and TreeView http://www.4guysfromrolla.com/articles/042308-1.aspx 以下文章将对XML,LINQ和TreeView有所帮助http://www.4guysfromrolla.com/articles/042308-1.aspx

There are also ways to do this without XML Data. 还有一些没有XML数据的方法。

You can still use Repeater like: 您仍然可以像这样使用Repeater:

<asp:Repeater ID="rpParent" runat="server" OnItemDataBound="rpParent_ItemDataBound">
    <ItemTemplate>
        <%# Eval("Name") %>
        <asp:Repeater ID="rpChild" runat="server">
            <ItemTemplate>
                <%# Eval("Name") %>
            </ItemTemplate>
        </asp:Repeater>
    </ItemTemplate>
</asp:Repeater>

Code Behind: 背后的代码:

public partial Page{
    protected void rpParent_ItemDataBound(object sender, RepeaterEventArgs e){
        Item ParentItem = e.Item.DataItem as Item;
        Repeater rpChild = e.Item.FindControl("rpChild") as Repeater;
        rpChild.DataSource = context.SelectChildObjectsByParentId(ParentItem.Id);
        rpChild.DataBind();
    }
}

Here is a pretty useful extension method for what you're trying to do. 这是您尝试执行的一种非常有用的扩展方法。 It will let you easily bind to a heirarchy and display using a treeview (or repeater if you want to do that too): 它可以让您轻松绑定到层次结构并使用树视图显示(如果您也想这样做,则可以使用转发器):

http://www.scip.be/index.php?Page=ArticlesNET18#AsHierarchy http://www.scip.be/index.php?Page=ArticlesNET18#AsHierarchy

http://www.scip.be/index.php?Page=ArticlesNET23 http://www.scip.be/index.php?Page=ArticlesNET23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM