Solved

Dynamic treeview node manipulation problem (and dynamic controls in general)

Posted on 2004-08-17
10
255 Views
Last Modified: 2012-05-05
i'd like to ask a side question about dynamic controls. I create a table and a bunch of controls locally in my own class (CBControlsSimple), and Add them to the Controls collection of panels on the main page

protected CBControlsSimple cbCont;
protected System.Web.UI.WebControls.Panel cbPanel;
protected System.Web.UI.WebControls.Panel zyPanel;

private void Page_Load(object sender, System.EventArgs e)
{
     if (!IsPostBack || Session.IsNewSession)
     {
          // create control sets
          cbCont = new CBControlsSimple(cbPanel,zyPanel);
          Session["CBCONT"] = cbCont;
     }
     else
     {
          cbCont = (CBControlsSimple)Session["CBCONT"];
          cbCont.ReconnectToPanel(cbPanel,zyPanel);
     }
     this.SmartNavigation = true;
}

And the CBControlsSimple::Reconnect function

     // Reconnect table to panel, for page reload
     public void ReconnectToPanel(Panel cbpan, Panel zypan)
     {
          HttpContext.Current.Response.Write("Reconnect controls to panel<br>");
          cbPanel = cbpan;
          cbPanel.Controls.Add(cbTable);
          zyCont.ReconnectToPanel(zypan); // yet another of my internal controls classes
     }

My question is this: I persist these objects by storing them in Session as you can see from my Page_Load(), specifically not creating the controls every time the page is loaded. But I am noticing strange behavior, such as when I try to add a TreeNode to my treeView (I add and remove nodes dynamically) I get an error stating

Attempted to add a TreeNode to a second TreeNodeCollection. A TreeNode can belong to only one TreeNodeCollection. Remove the TreeNode from its old collection before adding it to a new one.

What is going on!?
0
Comment
Question by:RichardFox
  • 4
  • 3
  • 3
10 Comments
 

Author Comment

by:RichardFox
ID: 11824743
Here is my treenode update code:

public void DisplayNode(string company_key)
{
// note: nodes are stored in a hash table. key is company name from list box

      if (compHash.Contains(company_key))
      {
            // node found
            found = (TreeNode)compHash[company_key];
            docTree.Nodes.Clear();
            docTree.Nodes.Add(found); // my treeview
      }
      else
      {
            docTree.Nodes.Clear();
      }
}
0
 
LVL 8

Accepted Solution

by:
trevorhartman earned 500 total points
ID: 11824810
do you know which line it gives an error on?

couple things i might try:
add a clone of the node:

docTree.Nodes.Add((TreeNode)found.Clone()); // my treeview

and see what happens.
also try using the insert method instead of add

docTree.Nodes.Insert(0, found);
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 11824868
I'm not sure about this, but I think you need to actually remove the nodes from their original tree.  The original tree for each is accessible from withing the node itself.  Try something like this:

TreeNode found = (TreeNode)compHash[company_key];
TreeNode node = found.OwnerTree.Remove( found );

This is just a general direction.  I can't recall the name of the owner tree property right now.
This problem is analogous to moving XmlNodes between different XmlDocuments.  They need to be imported from one to the other and cannot simply be "moved" over.

Sorry I can't offer any more suggestions.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:shovavnik
ID: 11824876
Hey, trevorhartman's suggestion to Clone the node is probably a much better and simpler solution.  I should have thought of that :)
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11824893
Yes, that's a good idea.  TreeView is the property to get the owner tree:

TreeNode found = (TreeNode)compHash[company_key];
TreeNode node = found.TreeView.Nodes.Remove( found );
0
 

Author Comment

by:RichardFox
ID: 11825164
Hmm. THere is no TreeView property in my TreeNode, there is a TreeBase.parent property but it exposes a very limited number of properties itself, Nodes is not included.

I am using Microsoft.Web.UI.WebControls.TreeView docTree, not the generic treeview.

Clone() works, though, except aren't I then leaving a lot of dangling references of TreeNodes to TreeViews?
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11825240
ok, i was looking at the System.Windows.Forms.TreeNode documentation...

It shouldn't leave references if you are just pulling the nodes out of a HashTable
0
 

Author Comment

by:RichardFox
ID: 11825320
I was thinking of the cloned nodes that I add to the docTree, and later remove. But I guess when I remove them they are gone, unless the framework keeps them on the heap somewhere. Which was my point. I just don't want to leave those cloned nodes floating out there. So you don't think it's a problem?
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11825340
I don't see it as a problem, but test it out to see if it has any affect on the performance of your app.
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 11839093
It doesn't keep them anywhere.  It has pretty smart garbage collection.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question