Auxiliary class that helps Treeview save and restore node expansion status Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI. webcontrols;
/// <Summary>
/// Heros Treeview auxiliary class for saving and recovering node expansion status
/// </Summary>
Public class treenodesexpandstatemanager: idisposable
{
Treeview _ tree;
List <treenode> nodes = new list <treenode> ();
Public static string sessionkey = "treenodesexpandstate ";
Public treenodesexpandstatemanager (Treeview tree)
{
_ Tree = tree;
}
/// <Summary>
/// Save the expanded status of the current tree
/// </Summary>
Public void saveexpandstate ()
{
Foreach (treenode node in this. _ tree. nodes)
{
Searchexpandednode (node );
}
}
Private void searchexpandednode (treenode node)
{
If (node = NULL)
Return;
Else
{
If (node. Expanded ?? False)
{
Nodes. Add (node );
Foreach (treenode item in node. childnodes)
{
Searchexpandednode (item );
}
}
}
}
/// <Summary>
/// Restore the expansion status of the current tree
/// </Summary>
Public void restoreexpandstate ()
{
Foreach (treenode node in this. _ tree. nodes)
{
Setnodeexpandstate (node );
}
}
/// <Summary>
/// Restore to the specified tree for cross-page operations
/// </Summary>
/// <Param name = "Tree"> </param>
Public void restoreexpandstatetotree (Treeview tree)
{
Foreach (treenode node in tree. nodes)
{
Setnodeexpandstate (node );
}
}
Private void setnodeexpandstate (treenode node)
{
If (node = NULL)
Return;
Else
{
If (this. nodes. Contains (node, new entity ()))
{
Node. Expand ();
Foreach (treenode item in node. childnodes)
{
Setnodeexpandstate (item );
}
}
}
}
/// <Summary>
/// Save the expanded state of the specified tree to the session, which is generally called in the expanded and closed events of the tree
/// </Summary>
/// <Param name = "Tree"> Target tree </param>
Public static void saveexpandstateinsession (Treeview tree, string sessionkey)
{
If (tree = NULL)
Return;
Treenodesexpandstatemanager manager = new treenodesexpandstatemanager (tree );
Manager. saveexpandstate ();
Httpcontext. Current. session [sessionkey] = manager;
}
/// <Summary>
/// Save the expanded state of the specified tree to the default session, which is generally called in the expanded and closed events of the tree
/// </Summary>
/// <Param name = "Tree"> Target tree </param>
Public static void saveexpandstateinsession (Treeview tree)
{
Saveexpandstateinsession (tree, sessionkey );
}
/// <Summary>
/// Restores the expanded state of the node in the session to the specified tree and calls
/// </Summary>
/// <Param name = "Tree"> Target tree </param>
Public static void restoreexpandstatetotreefromsession (Treeview tree, string sessionkey)
{
If (httpcontext. Current. session [sessionkey] = NULL | tree = NULL)
Return;
Treenodesexpandstatemanager manager = httpcontext. Current. session [sessionkey] As treenodesexpandstatemanager;
Manager. restoreexpandstatetotree (tree );
}
/// <Summary>
/// Restore the node expansion status in the default session to the specified tree and call
/// </Summary>
/// <Param name = "Tree"> Target tree </param>
Public static void restoreexpandstatetotreefromsession (Treeview tree)
{
Restoreexpandstatetotreefromsession (tree, sessionkey );
}
# Region idisposable Member
Public void dispose ()
{
This. nodes. Clear ();
This. nodes = NULL;
This. _ tree. Dispose ();
}
# Endregion
~ Treenodesexpandstatemanager ()
{
This. Dispose ();
}
Public class extends entity: iequalitycomparer <treenode>
{
# Region iequalitycomparer <treenode> Member
Public bool equals (treenode X, treenode y)
{
If (x = NULL | Y = NULL)
Return false;
Return X. value. Equals (Y. value );
}
Public int gethashcode (treenode OBJ)
{
Return obj. gethashcode ();
}
# Endregion
}
}