Adding the same TreeNode to multiple TreeNodes

G

Guest

When I attempt to run the following code:

protected void NavigationTreeView_TreeNodePopulate(object sender,
TreeNodeEventArgs e) {
TreeNode tn1 = new TreeNode("node1","node1");
TreeNode tn2 = new TreeNode("node2", "node2");
TreeNode newNode = new TreeNode("add me to both nodes", "add me to
both nodes");
e.Node.ChildNodes.Add(tn1);
e.Node.ChildNodes.Add(tn2);
tn1.ChildNodes.Add(newNode);
tn2.ChildNodes.Add(newNode);
}

It does not behave as expected. Instead of adding a copy of newNode to each
of the other two nodes, it adds to tn1 and then when it adds it to tn2 it
removes it from tn1, thus in the end only tn2 has the newNode and tn1 has no
children. This surprises me. How can I add the same node to two separate
locations in a treeview?
 
G

Guest

Sorry...I should have noted that this is the web control:
System.Web.UI.WebControls.TreeNode
 
A

AMDRIT

Is this from ASP.Net 2.0? It appears that both clone and
memberwiseclone are available.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.treen
ode.clone.aspx

If not, you should be able to easily derive a new component that exposes
iclonable.

However, cloning is essentially creating a new copy of an existing node.
Perhaps simple procedural logic will suffice.

private sub CreateNode(PNode as node, NodeName as string, Tag as object)

dim cNode as treenode
cNode = pnode.nodes.add(NodeName)
cNode.tag = Tag

end sub

Private sub CloneNode(PNode as treenode, CNode as treenode)

dim newNode as treenode

newNode = pNode.nodes.Add(cnode.text)

with newNode
.Tag = CNode.Tag
'Add other property states here
end with

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top