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

时间:2014-09-06 07:17:28

标签: c# asp.net

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

例如..

root(2)

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

1 个答案:

答案 0 :(得分:0)

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

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]);
    }
}