The treeview control has a lot going for it, but it is lacking any sort of ability to maintain state of the currently selected node. This becomes an issue when you want to change/maintain a property on a selected node.
An example would be a treeview of urls where you want to set the backcolor of the selected node after navigating to the new page.
The only way I found to resolve this issue is to store the node.valuepath in a session variable and reselect it during the page_load event.
First, make sure none of your urls are in the NavigateUrl property. This puts the treeview into “Navigation” mode and results in the OnSelectedNodeChanged event not firing. Urls go in the Value property.
Be sure to change your treeview PathSeparator to something like “|” or something other than the default “/”. If you forget to change the default value, it will cause all sorts of problems when using FindNode.
Next, add some code to catch the selectednode.valuepath
Private Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
Session("selected") = TreeView1.SelectedNode.ValuePath
Response.Redirect(TreeView1.SelectedNode.Value, "false")
End Sub
Finally, add code to the pageload or init event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("selected") <> "" Then
TreeView1.FindNode(Session("selected")).Select()
End If
End Sub