>So far the TreeView control in ASP.NET 2.0 seems even more half-baked than most of the ASP.NET controls.
>
>I'm working on an application that uses a treeview for navigation through several layers of heirarchical data. The application inserts/updates/deletes records in that data.
>
>Building/rebuilding and displaying the treeview is no problem.
>
>Problem comes when I am deep in the treeview and edit a record and want to rebuild the treeview to show updated data. I want to rebuild the treeview and then programmatically select the node I was working on (or select the parent if I deleted or inserted a node).
>
>How do I programmatically navigate back to and select a given node after reloading a treeview?
>
>I stupidly assumed this should be a trivial task and waited till almost the end of the project to do this.
>
>This is in VS2005/ASP.NET 2.0/VB.NET.
After a LOT of searching (and thanks to those who tried to help), I finally managed to get this to work. I may have to start a blog to try to post .NET code that is actually working rather than fragmented, incomplete stuff.
Basically, I have a node expanded and selected in my treeview somewhere down the heirarchy and I now want to rebuild my treeview and expand to and select the same node again.
'Before I rebuild the treeview I get the selected Node's ValuePath..
Dim SavedValuePath as string = myTreeView.SelectedNode.ValuePath
'Then I rebuild my treeview (you gotta figure that out for your situation).
'After rebuilding it, I find my node again - note I HAVE TO EXPAND THE ENTIRE TREEVIEW IN ORDER FOR FINDNODE METHOD TO WORK!!
MyTreeView.ExpandAll()
Dim ReturnNode as TreeNode = ctype(MyTreeView.FindNode(SavedValuePath),TreeNode)
'Now I collapse my TreeView since I don't want the whole thing expanded
MyTreeView.CollapseAll()
'Now I need to expand the treeview down to my node - I do it by Expanding each parent branch from my node UP.
Dim ParentNode as TreeNode = ReturnNode
While ParentNode.Parent IsNot Nothing
ParentNode.Expand()
ParentNode = ParentNode.Parent
end while
ParentNode.Expand()
ReturnNode.Expand()
Viola!
What a pain, but it works!!
____________________________________
Don't Tread on Me
Overthrow the federal government NOW!
____________________________________