TreeView Delete node asp.net code

Source: Internet
Author: User
Tags instance method

TreeView Delete Node ASP tutorial. NET code
private bool Isin (treenodecollection tnodes,string s)
{
foreach (TreeNode td in Tnodes)
{
if (Td.name = = s) return true;
if (Td.nodes.count > 0)
if (Isin (Td.nodes, s)) return true;
}
return false;

}

Instance method

private void Addtreenode ()
{
Determine if there are duplicate names
if (Traversetreenode (string rename))
{
MessageBox.Show ("This node already exists!") "," hint ");
}
Else
{
Add a node (I don't know where you want to find it)
}
}
private bool Traversetreenode (string rename)
{
Whether there is a duplicate name between the Add node and the child node of the current node
if (This.treeroom.selectednode.nodes.count > 0)
{
foreach (TreeNode node in this.treeroom.selectednode.nodes)
{
if (rename = = Node.text)
return true;
}
return false;
}
Whether there is a duplicate name between the Add node and the sibling of the current node (if the parent node of the current node exists)
else if (This.treeroom.selectednode.parent.nodes.count > 0)
{
foreach (TreeNode node in this.treeroom.selectednode.parent.nodes)
{
if (Rename.compareto (node.text) = = 0)
return true;
}
return false;
}
}
Issue 2: Deleting a treeview node if there are child nodes is not allowed
private void Deltreenode ()
{
Determine if there are child nodes
if (this.treeroom.selectednode.nodes.count>0)
{
MessageBox.Show ("This node contains child nodes!") "," hint ");
}
Else
{
Remove the current node
This.treeroom.selectednode.remove ();
}
}
Add:
Question 1: Do not know you add the node name to determine whether in the whole tree, if that need to traverse the entire tree.
private bool Traversetreenode (string rename)
{
BOOL flag = FALSE;
foreach (TreeNode node in treeroom.nodes.nodes)
{
if (node.nodes.count>0)
{
Flag = Traversetreenode (rename);
if (flag)
return true;
}
Else
{
if (Rename.equals (Node.text))
return true;
}
}
return false;
}

//

Send a function to find node

<summary>
Traverses the tree control and finds the node with the text code. Using recursive algorithm
</summary>
<param name= "Nodes" > node set to find </param>
<param name= "Parentcode" > matching conditions, for node text</param>
<returns></returns>
Private TreeNode Findnodebycode (treenodecollection nodes, string code)
{

if (nodes.count = 0)
return null;

foreach (TreeNode node in nodes)
{
Determine if this node is a target
if (node.text.tostring () = = Code.trim ())
return node;

Whether this node has child nodes
TreeNode node_2;

if (node.childnodes.count!= 0)
{
Has child nodes, the recursive call begins
node_2 = Findnodebycode (node.childnodes, code);
}
Else
{
No child nodes
node_2 = null;
}
Returns the target if the target is found recursively
if (node_2!= null)
return node_2;

This node and the child nodes of the node (including all child nodes) do not contain the target, and the next node loop
}

Traversed all nodes, failed to find the target, had to return null
return null;

}
}

Related Article

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.