我写了一些代码:
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'中的行,但我无法管理它。我需要创建站点地图,其中将表示所有子节点。我的代码只返回第一级而不是所有子行。我应该如何解决它?
答案 0 :(得分: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;
}