How to set SelectedNode and set focus of selected node in Telerik RadTreeView?

I am using Telerik RadTreeView with ASP.Net C #. I can install the selected Node using the following code:

        var node = radTreeViewMenuStructure.Nodes.FindNodeByValue(linkID.ToString());

        if (node != null) // <- equals null when not on the root of the tree
        {
            node.Selected = true;
            node.Expanded = true;
            node.ExpandParentNodes();
            node.Focus();
        }

The above code sets the selected Node only if the Node is outside the root and not enclosed in the parent node. My Node = null when choosing the Node ID, which is enclosed in the parent node. Any suggestions?

+3
source share
3 answers

.FindNodeByValue is looked up in tree nodes. He does not look at every child node. The solution was to walk tree recursively. Here is my code that finally solved the problem:

    private void SelectLink(int linkID, RadTreeNodeCollection rootNodes)
    {
        var node = rootNodes.FindNodeByValue(linkID.ToString());
        if (node != null)
        {
            node.Selected = true;
            node.Expanded = true;
            node.ExpandParentNodes();
            node.Focus();

            ... Do some other work ...

            return;
        }

        // for each node with children  
        foreach (RadTreeNode item in rootNodes.Cast<RadTreeNode>().Where(item => item.Nodes.Count > 0))
        {
            // Recursive call to self to walk the tree
            SelectLink(linkID, item.Nodes);
        }
    }

Then I just call the method with the root of RadTreeView:

SelectLink(radTreeViewMenuStructure.Nodes, idToFind);
+3
source

node.ExpandParentNodes();

+1

: radTreeViewMenuStructure.FindNodeByValue() .

0
source

All Articles