Hi I have a TreeView object created on my website using the following code:

In my Page_Load method I call BuildDocumentNavigationTree() as in:

private void BuildDocumentNavigationTree()
    {
        TreeNode rootNode = new TreeNode(currentDocumentStructure.Label);
        rootNode.Value = currentDocumentStructure.UniqueId.ToString();
        rootNode.Value = rootNode.Value + " : " + currentDocumentStructure.Title.ToString();
        AddChildElementNodes(rootNode, currentDocumentStructure);
        TreeView1.Nodes.Add(rootNode);
    }

    private static void AddChildElementNodes(TreeNode parentNode, DocumentElementType element)
    {
        foreach (DocumentElementType childElement in element.DocumentElements)
        {
            TreeNode elementNode = new TreeNode(childElement.Label);
            elementNode.Value = childElement.UniqueId.ToString();
            elementNode.Value = elementNode.Value + " : " + childElement.Title.ToString();
            AddChildElementNodes(elementNode,childElement);
            parentNode.ChildNodes.Add(elementNode);
            elementNode.CollapseAll();
        }
    }

This will build my tree successfully and it has quite a few nodes with child nodes etc. Basically a user can navigate through the tree and select a document, and that document will display on screen when selected. They can then click 'Add Comments' which will bring them to a new page to add their comments to that document. On the add comments screen the user can click cancel which brings them back to the document screen, although the tree fully collapses and no longer displays the document.

I therefore need the tree to return to the previously selected state and also display the document of that selected state. Can someone please help me?

Just depends on how you want to handle it. If transferring information to caller as node's text, you can do a search of all nodes to find it:

TreeNode FindTreeNodeText(TreeNodeCollection nodes, string findText)
            {
                TreeNode foundNode = null;
                for (int i = 0; i < nodes.Count && foundNode == null; i++)
                {
                    if (nodes[i].Text == findText)
                    {
                        foundNode = nodes[i];
                        break;
                    }
                    if (nodes[i].Nodes.Count > 0)
                        foundNode = FindTreeNodeText(nodes[i].Nodes, findText);
                }
                return foundNode;
            }

If returning the selected node to the caller, you can select it directly:

void SelectNode(TreeNode node)
            {
                treeView1.SelectedNode = node;
            }

and, to know which node is selected to return to caller:

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                TreeNode node = treeView1.SelectedNode;
            }

This method of assigning a TreeNode to TreeView.selectedNode does not work, TreeView.selectedNode is read-only (it cannot be assigned to).

Hi I managed to get it working. I used a session variable to store the selected TreeNode. Then when the page reloads it searchs the tree for the session treeNode. It then expands the found childNode and each of its parent nodes, ie:

private TreeNode ExpandSelectedNode(TreeNode rootNode)
    {
        foreach (TreeNode childNode in rootNode.ChildNodes)
        {
            if (childNode.Value.Equals(((TreeNode)Session["Node"]).Value))
            {
                childNode.Expand();
                ExpandParentNode(childNode);
            }
            ExpandSelectedNode(childNode);
        }
        return rootNode;
    }

    private void ExpandParentNode(TreeNode childNode)
    {
        if (childNode.Parent != null)
        {
            childNode.Parent.Expand();
            ExpandParentNode(childNode.Parent);
        }
    }

This method of assigning a TreeNode to TreeView.selectedNode does not work, TreeView.selectedNode is read-only (it cannot be assigned to).

I guess you are using the WebControls version of the TreeView. Look at the MSDN documentation for setting selection: Select node...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.