C. Sura
C. Sura

Reputation: 98

ASP TreeView: All nodes have the same ID

I'm currently writing a custom web forms control with, among others, an embedded TreeView control. The content of this TreeView control is generated programmatically like this:

    public void PopulateTreeView(TreeView treeView)
    {
        // Initialize with a single root node.
        treeView.Nodes.Clear();
        TreeNode currentNode = new TreeNode("root");
        treeView.Nodes.Add(currentNode);

        // Some logic with basically a depth first search through some data classes.
        {
            TreeNode node = new TreeNode(/* node label */);
            currentNode.ChildNodes.Add(node);

            if ( /* some condition */)
            {
                currentNode = node;
            }
        }

    }

When rendering, everything looks fine, except that the ID for all HTML elements generated is ControlName1n0, which has the effect that clicking on the expand/collapse icons will only expand/collapse the root node.

Does anyone know what's the cause of this or how to fix this?

Upvotes: 0

Views: 665

Answers (1)

C. Sura
C. Sura

Reputation: 98

Moving the code to create the TreeNodes from the Render method to the OnPreRender method solved the problem; now all nodes get a unique ID.

Upvotes: 0

Related Questions