The problem
A treenode can be selected or expanded. Selecting the node causes the SelectedNodeChange event to fire if the Treeview is configured correctly. Clicking the + icon on a node causes the TreeNodeExpanded event to fire. The selected event will not fire if the node is in Navigation mode. This behavior makes sense most of the time. The node is acting as a navigation link - it causes the browser to take you to a new page which means there is no postback. Your are going to a new page, without returning to the server first.
If the node has its NavigationUrl property set to an empty string, the node is in Selection mode. If the NavigationUrl is a non-zero length string ,the node is in Navigation mode.
For our project I wanted the children nodes to expand whenever the user selects a parent node. Yes, the user can click the + symbol to expand it, but our testing showed that many users expect the node to expand by clicking the node text instead. Since the nodes are in Navigation mode I couldn't put the code in the SelectedNodeChanged event.
My solution? Use the TreeNodeDatabound event to examine each node as it is being bound to the tree. If the current page URL matches the treenode NavigationUrl I expand all the of the current nodes children. It solves my problem. It still doesn't cause the SelectedNodeChanged to fire so it may not solve all your troubles. At least it's a start.
Protected Sub treeMainMenu_TreeNodeDataBound _
(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) _
Handles treeMainMenu.TreeNodeDataBound
' other binding code here...
If Request.Url.PathAndQuery = e.Node.NavigateUrl Then
e.Node.ExpandAll()
End If
End Sub
No comments:
Post a Comment