Treeview control save and restore node expansion status

Source: Internet
Author: User
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
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.