Home - Forums-.NET - FlyTreeView (ASP.NET) - Populating TreeNodeTypeCollections

FlyTreeView (ASP.NET)

Technical support and KB related to the FlyTreeView control

This forum related to following products: FlyTreeView for ASP.NET

Populating TreeNodeTypeCollections
Link Posted: 12-Sep-2005 02:34
I have a bunch of NodeTypes that I want to use throughout my project, in several different FTVs, without having to redefine them everytime.
I'm trying to do this by defining a class, which contains a static NodeTypeCollection, initialized by a static constructor:


  public class TreeView {
    static public readonly Ftv.TreeNodeTypeCollection NodeTypes;    

    static TreeView() {
      //Create node types
      NodeTypes = new Ftv.TreeNodeTypeCollection();
      Ftv.TreeNodeType nt1 = new Ftv.TreeNodeType();
      nt1.Name = "nt1";
      NodeTypes.Add(nt1);

      Ftv.TreeNodeType nt2= new Ftv.TreeNodeType();
      nt2.Name = "nt2";
      NodeTypes.Add(nt2);
    }
  }
}


In an ASPX page, I then add a FlyTreeView, and try and populate its NodeTypes TreeNodeTypeCollection from the static one defined above:

FlyTreeView ft;

// ... some stuff here

// In the Page_Load event handler:
foreach(TreeNodeType nt in TreeView.NodeTypes) ft.NodeTypes.Add(nt);


The Add method on the last line throws an exception: "This object is already in collection", when ft.NodeTypes is indeed empty before the call.
This does not happen if nt is a locally defined NodeType (in the Page_Load handler).

Any idea what can cause this ?
Any smarter way of achieving the same result (defining [once] a bunch of TreeNodeTypes that can be re-used in several pages) ?

Thanks,
Pyt.
Link Posted: 12-Sep-2005 18:57
Actually the reason of this behavior is in the NodeTypeCollection class implementation that does not allow a single NodeType instance to be placed in several NodeTypeCollection instances.

To implement required behavior you may try to implement the following:

1. Use Clone() method of flytreeview to workaround the error:

foreach(TreeNodeType nt in TreeView.NodeTypes) ft.NodeTypes.Add((TreeNodeType)(nt.Clone()));



2. Create child class of FlyTreeView having custom constructor and use it instead of original:

public class CustomTreeView : FlyTreeView {
    public CustomTreeView() : base() {
         NodeTypes = new Ftv.TreeNodeTypeCollection();
         Ftv.TreeNodeType nt1 = new Ftv.TreeNodeType();
         nt1.Name = "nt1";
         NodeTypes.Add(nt1);

         Ftv.TreeNodeType nt2= new Ftv.TreeNodeType();
         nt2.Name = "nt2";
         NodeTypes.Add(nt2);
    }
}


Hope this helps.
Link Posted: 12-Sep-2005 21:52
Most helpful. Thanks !

Pyt.