在asp.net树视图的每个分支中添加子节点的计数

本文关键字:添加 子节点 分支 net asp 视图 | 更新日期: 2023-09-27 18:20:55

我想在asp.net树视图的每个分支中添加子节点的计数。

例如。。

根(2)

 parent (3)
   child
   child
   child
parent (2)
   child
   child

在asp.net树视图的每个分支中添加子节点的计数

您需要使用代码隐藏。你可以做:

void Page_Load(Object sender, EventArgs e)
{
    // Iterate through the root nodes in the Nodes property.
    for(int i=0; i<YourTreeView.Nodes.Count; i++)
    {
        // Set the text to include children count.
        SetChildNodeText(YourTreeView.Nodes[i]);
    }
}
void SetChildNodeText(TreeNode node)
{
    if(node.ChildNodes.Count > 0)
    {
        node.Text += ' (' + node.ChildNodes.Count.ToString() + ')'; // append child node count to the text
    }
    for(int i=0; i<node.ChildNodes.Count; i++)
    {
        // recursion
        SetChildNodeText(node.ChildNodes[i]);
    }
}