简体   繁体   English

asp:SQL中具有父子关系的TreeView

[英]asp:TreeView with parent-child relationship in SQL

I wrote some code: 我写了一些代码:

 protected void RootNodes()
{
    var contents = ListContentRootNodes(0, this.Culture, true);
    foreach (var content in contents)
    {
        TreeNode newNode = new TreeNode
        {
            Value = content.Title
        };

        List<TreeNode> childNode = ChildNode(content.ContentId);
        if (childNode != null)
        {
            foreach (var item in childNode)
            {
                newNode.ChildNodes.Add(item);
            }
            treeView.Nodes.Add(newNode);
        }
        treeView.ExpandAll();
    }
}

protected List<TreeNode> ChildNode(int contentId)
{
    var subContents = ListContentChildNodesAll(contentId, this.Culture);

    List<TreeNode> nodes = new List<TreeNode>();
    foreach (var sub in subContents)
    {
        nodes.Add(new TreeNode { Value = sub.Title });
        ChildNode(sub.ContentId);
    }
    return nodes;
}

'ListContentRootNodes' returns first level data rows with 'ParentId' set to '-1' . “ ListContentRootNodes”返回将“ ParentId”设置为“ -1”的第一级数据行。 ListContentChildNodesAll calling stored procedure: ListContentChildNodesAll调用存储过程:

CREATE PROCEDURE [dbo].[ListChildNodesAll]
@ContentId int,
@Culture nvarchar(5)

AS SET NOCOUNT ON
SELECT * FROM Content WHERE ParentId=@ContentId AND Culture=@Culture

As far as I searched on net, I've understood that I need recursion method in order to populate rows from 'DB' , but I could not managed it. 就我在网上搜索而言,我了解到我需要递归方法才能从'DB'填充行,但是我无法对其进行管理。 I need to create sitemap where all subnodes would be represented. 我需要创建将表示所有子节点的站点地图。 My code is returning only first level and not all child rows. 我的代码仅返回第一级,而不是所有子行。 How should I fixed it? 我该如何解决?

I finally find solution.My complete working code is : 我终于找到了解决方案,我完整的工作代码是:

 protected void RootNodes()
{
    var contents = ListContentRootNodes(0, this.Culture, true);
    foreach (var content in contents)
    {
        TreeNode newNode = new TreeNode { Value = content.Title };

        List<TreeNode> childNode = ChildNode(content.ContentId);
        if (childNode != null)
        {
            foreach (var item in childNode)
            {
                newNode.ChildNodes.Add(item);
            }
            treeView.Nodes.Add(newNode);
        }
        treeView.ExpandAll();
    }
}

protected List<TreeNode> ChildNode(int contentId)
{
    var subContents = ListContentChildNodesAll(contentId, this.Culture);
    List<TreeNode> nodes = new List<TreeNode>();

    foreach (var sub in subContents)
    {
        TreeNode nod = new TreeNode { Value = sub.Title };
        nodes.Add(nod);
        List<TreeNode> leafNodes = ChildNode(sub.ContentId);
        foreach (var leaf in leafNodes)
        {
            nod.ChildNodes.Add(leaf);
        }
    }
    return nodes;
}

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

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