title: Creating custom TreeView/TreeNode
body: <p>I need to extend the TreeNode class such that I can add custom properties to each node (seeing as WebForms TreeNode doesn't include the Tag property). So this is my CustomTreeNode:</p>
  
  <pre><code>public class CustomTreeNode : TreeNode
  {
  
      public CustomTreeNode()
      {               
      }
  
      public CustomTreeNode(int nodeId, string nodeType)
      {
          NodeId = nodeId;
          NodeType = nodeType;
      }
  
      public string NodeType { get; set; }
      public int NodeId { get; set; }
  }
  </code></pre>
  
  <p>If I create a CustomTreeNode and add it to a TreeView:</p>
  
  <pre><code>CustomTreeNode node = new CustomTreeNode(1, "CustomType");            
  treeView.Nodes.Add(node);
  </code></pre>
  
  <p>I would then get a casting exception doing the following:</p>
  
  <pre><code>CustomTreeNode selectedNode = (CustomTreeNode)TreeView.SelectedNode;
  </code></pre>
  
  <p>because TreeView returns a TreeNode, not a CustomTreeNode.</p>
  
  <p>I've done some reading, and it looks like I need to extend the TreeView class, and override the CreateNode() method to return CustomTreeNode instead of TreeNode. So I created this:</p>
  
  <pre><code>public class CustomTreeView : TreeView
  {
      protected override TreeNode CreateNode()
      {
          return new CustomTreeNode();
      }
  }
  </code></pre>
  
  <p>The problem is however, CreateNode() doesn't take any arguments, so you have to have call the empty constructor for the CustomTreeNode class. So when I created my CustomTreeNode above, when I get it back from the CustomTreeView, the nodeId and nodeType values have been lost because the empty constructor returns a node without any values.</p>
  
  <p>Any help much appreciated.</p>