Web Site

 Resolve Nodes Changes

Keep ASTreeView free, keep ASTreeView update. Please make a donation to support the development.
  • English
  • 中文
Common Assembly Version: 1.5.9.0

Target (split by ",")
This demo shows how to traverse all the nodes of the treeview, and how to get the modified nodes including "modified node", "parent changed node", "deleted node".
CONSOLE
cs code
#region Nodes Iteration

        protected void btnIterateNodes_Click( object sender, EventArgs e )
        {
            string result = string.Empty;

            IterateNode( this.astvMyTree.RootNode, ref result );

            this.divConsole.InnerHtml += ( string.Format( ">>nodes iteration: <div style='padding-left:20px;'>{0}</div>", result ) );

        }

        private void IterateNode( ASTreeViewNode node, ref string nodeString )
        {
            if( !node.Equals( this.astvMyTree.RootNode ) )
                nodeString += ( "[NODE:]" + node.NodeText + "[PARENT-NODE:]" + node.ParentNode.NodeText + "<br />" );

            foreach( ASTreeViewNode child in node.ChildNodes )
            {
                //recursive call
                IterateNode( child, ref nodeString );
            }
        }

        #endregion

        #region TraverseNodes

        protected void btnTraverseNodes_Click( object sender, EventArgs e )
        {
            string result = string.Empty;

            ASTreeView.ASTreeNodeHandlerDelegate nodeDelegate = delegate( ASTreeViewNode node )
            {
                if( !node.Equals( this.astvMyTree.RootNode ) )
                    result += ( "[NODE:]" + node.NodeText + "[PARENT-NODE:]" + node.ParentNode.NodeText + "<br />" );
            };

            astvMyTree.TraverseTreeNode( this.astvMyTree.RootNode, nodeDelegate );

            this.divConsole.InnerHtml += ( string.Format( ">>nodes traverse: <div style='padding-left:20px;'>{0}</div>", result ) );

        }

        #endregion

        #region Resolve ASTreeView Nodes' Modification

        protected void btnResolveNodesModification_Click( object sender, EventArgs e )
        {
            ResolveTreeNodesModification();
        }

        /// <summary>
        /// save the original tree nodes to session for comparison later
        /// </summary>
        private void SaveOriginalTreeNodes()
        {
            //create a hashtable to store nodes
            Hashtable ht = new Hashtable();

            ASTreeView.ASTreeNodeHandlerDelegate nodeDelegate = delegate( ASTreeViewNode node )
            {
                //skip RootNode
                if( node.Equals( this.astvMyTree.RootNode ) )
                    return;

                //assume NodeValue is unique
                ht.Add( node.NodeValue, node );
            };

            astvMyTree.TraverseTreeNode( this.astvMyTree.RootNode, nodeDelegate );

            //set the session
            this.OriginalTreeNodes = ht;
        }

        /// <summary>
        /// compare to the OriginalTreeNodes to find the nodes which have been changed
        /// </summary>
        private void ResolveTreeNodesModification()
        {
            //the result string
            StringBuilder sb = new StringBuilder();

            #region traverse all the nodes, detect new nodes, modified nodes
            //traverse all the nodes, detect new nodes, modified nodes
            ASTreeView.ASTreeNodeHandlerDelegate nodeDelegate = delegate( ASTreeViewNode node )
            {
                //skip RootNode
                if( node.Equals( this.astvMyTree.RootNode ) )
                    return;

                object obj = this.OriginalTreeNodes[node.NodeValue];
                //if node is a new node, it can't be found in the OriginalNodes
                if( obj == null )
                    sb.Append( string.Format( "[NEW] Node: {0} <br />", node.NodeText ) );
                else
                {
                    ASTreeViewNode originalNode = (ASTreeViewNode)obj;

                    //if the node has been changed
                    //compare, here I just demo NodeText and ParentNodeId
                    if( node.NodeText != originalNode.NodeText )
                        sb.Append( string.Format( "[TEXT CHANGED]NodeText changed! Original Text:{0}, New Text: {1} <br />", originalNode.NodeText, node.NodeText ) );

                    if( node.ParentNode.NodeValue != originalNode.ParentNode.NodeValue )
                        sb.Append( string.Format( "[PARENT CHANGED]Node's parent changed! Original Parent: {0}, New Parent: {1} <br />", originalNode.ParentNode.NodeText, node.ParentNode.NodeText ) );

                }
            };

            //do traverse
            astvMyTree.TraverseTreeNode( this.astvMyTree.RootNode, nodeDelegate );

            #endregion


            #region find deleted nodes

            //find deleted nodes
            List<string> originalNodesKeys = new List<string>();
            foreach( string key in this.OriginalTreeNodes.Keys )
                originalNodesKeys.Add( key );

            //traverse all the nodes, detect new nodes, modified nodes
            ASTreeView.ASTreeNodeHandlerDelegate nodeDeleteDelegate = delegate( ASTreeViewNode node )
            {
                //skip RootNode
                if( node.Equals( this.astvMyTree.RootNode ) )
                    return;

                if( originalNodesKeys.Contains( node.NodeValue ) )
                    originalNodesKeys.Remove( node.NodeValue );
            };

            //do traverse
            astvMyTree.TraverseTreeNode( this.astvMyTree.RootNode, nodeDeleteDelegate );

            //keys remain in the originalNodesKeys are the deleted nodes
            foreach( string deletedNodeKey in originalNodesKeys )
            {
                sb.Append( string.Format( "[NODE DELETED]Original Node: {0}<br />", deletedNodeKey ) );

            }

            #endregion

            this.divConsole.InnerHtml += ( string.Format( ">>The following nodes have been changed: <div style='padding-left:20px;'>{0}</div>", sb.ToString() ) );

            
        }

        #endregion
 


Dear friend,

    ASTreeView license has been updated, please visit the License & Pricing page for detial. Thank you.