954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with selecting a TreeView node dynamically

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?

moorcroft
Newbie Poster
18 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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;
            }
DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

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

moorcroft
Newbie Poster
18 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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);
        }
    }
moorcroft
Newbie Poster
18 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
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...

DdoubleD
Posting Shark
996 posts since Jul 2009
Reputation Points: 341
Solved Threads: 233
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: