1

我写了一些代码:

 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'返回'ParentId'设置为'-1'的第一级数据行。ListContentChildNodesAll调用存储过程:

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

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

据我在网上搜索,我了解到我需要递归方法才能从'DB'填充行,但我无法管理它。我需要创建将表示所有子节点的站点地图。我的代码只返回第一级而不是所有子行。我应该如何修复它?

4

1 回答 1

0

我终于找到了解决方案。我完整的工作代码是:

 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;
}
于 2013-04-06T09:53:47.997 回答