After populating the TreeView, either expand or collapse all of the nodes to insure that there is a consistent default behavior. Then restore your TreeView expansion state to a previously saved state by invoking the RestoreTreeView method of the TreeViewState class:
// set the default state of all nodes.
TreeViewMain.CollapseAll();
// get the saved state of all nodes.
new TreeViewState().RestoreTreeView(TreeViewMain, this.GetType().ToString());
Before navigating to a new page, save the existing state for future use but first you need to overcome the urge to use the NavigateUrl property on a TreeNode. Using this property will generate an HTML hyperlink tag on your TreeView which will cause the browser to initiate the transfer to the new Url. Control is not returned to the server and you will never have the opportunity to save the existing state. Instead, wire up the TreeView_SelectedNodeChanged event for the TreeView, put your page address into the Node.Value property and use a Response.Redirect():
protected void TreeViewMain_SelectedNodeChanged(object sender, EventArgs e)
{
if (TreeViewMain.SelectedNode.Value != string.Empty)
{
Response.Redirect(TreeViewMain.SelectedNode.Value);
}
}
protected void TreeViewMain_Unload(object sender, EventArgs e)
{
// save the state of all nodes.
new TreeViewState().SaveTreeView(TreeViewMain, this.GetType().ToString());
}
Within the SaveTreeView and RestoreTreeView methods of the class, recursively walk the nodes collection and either save or restore the TreeNode.Expanded property in/from a generic list of type <bool?>. The TreeNode uses a nullable boolean value to store this state so I used the same type in a List.
The expansion state is saved in a Session variable. It is possible that you will want to save the state for two identically named TreeView controls on different master pages within your application. This would cause an issue with the name of the Session variable so I included a 'key' parameter that is concatenated with the ID of the TreeView to form the name of the session variable. You can pass any string value as the key, but using the name of the invoking master page class should eliminate most conflicts. I coded this as 'this.GetType().ToString()' which will return a string of the class type.
Lastly, if the number of nodes exceeds the element count in the List<> object, I simply return from the method. This might happen if the data source for your TreeView changes on the fly. If your data source if routinely changing, you will need to tweak the class to better handle this scenario.
Here is the complete CS class.
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
public class TreeViewState
{
public void SaveTreeView(TreeView treeView, string key)
{
List<bool?> list = new List<bool?>();
SaveTreeViewExpandedState(treeView.Nodes, list);
HttpContext.Current.Session[key + treeView.ID] = list;
}
private int RestoreTreeViewIndex;
public void RestoreTreeView(TreeView treeView, string key)
{
RestoreTreeViewIndex = 0;
RestoreTreeViewExpandedState(treeView.Nodes,
(List<bool?>)HttpContext.Current.Session[key + treeView.ID] ?? new List<bool?>());
}
private void SaveTreeViewExpandedState(TreeNodeCollection nodes, List<bool?> list)
{
foreach (TreeNode node in nodes)
{
list.Add(node.Expanded);
if (node.ChildNodes.Count > 0)
{
SaveTreeViewExpandedState(node.ChildNodes, list);
}
}
}
private void RestoreTreeViewExpandedState(TreeNodeCollection nodes, List<bool?> list)
{
foreach (TreeNode node in nodes)
{
if (RestoreTreeViewIndex >= list.Count) break;
node.Expanded = list[RestoreTreeViewIndex++];
if (node.ChildNodes.Count > 0)
{
RestoreTreeViewExpandedState(node.ChildNodes, list);
}
}
}
}
thuong HoangPosted Apr 25, 2011, 5:34 AM
it's very usefull!
Kardo SabourPosted Jun 20, 2010, 6:23 AM
Hi Vimal, Nice code. It works fine for the static nodes you've provided, but when adding dynamic nodes using PopulateOnDemand, it doesn't save the state of those dynamics. But when I expand the dynamic nodes, and then I expand one of those statics, then I click on that expanded static node it saves the whole treenode's status. Any suggestion? Kardo