Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Inherited TreeView Nodes and typeof

Inherited TreeView Nodes and typeof

Scheduled Pinned Locked Moved ASP.NET
csharpasp-nethelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    perlmunger
    wrote on last edited by
    #1

    I am using an ASP.NET 2.0 TreeView and was wanting to create different child TreeNode types for the different items that I am loading into the treeview. The idea behind this is that all I should have to do is check the node type when a node gets clicked and I can then respond accordingly. The problem, however, is that the child treenode I created that inherits from TreeNode gets reported to be a normal TreeNode type rather than the inherited treenode type when I use the typeof operator even though I instantiate it as the child TreeNode type. Am I missing something?

    class Child1TreeNode : TreeNode
    {
    }

    class Child2TreeNode : TreeNode
    {
    }

    And then in the Page Load:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    Child1TreeNode node = new Child1TreeNode();
    node.Text = "Hello World";
    node.Value = "1";
    Child2TreeNode node2 = new Child2TreeNode();
    node2.Text = "Goodbye World";
    node2.Value = "2";
    TreeNode topNode = TreeView.FindNode("Top");
    topNode.ChildNodes.Add( node );
    }
    }

    And then in the event handler for node changed:

    protected void TreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
    TreeNode node = ((TreeView)sender).SelectedNode;
    if (node.GetType() == typeof(Child1TreeNode) )
    {
    // Do something related to Child1TreeNode types
    }
    else if (node.GetType() == typeof(Child2TreeNode) )
    {
    // Do something related to Child2TreeNode types
    }
    }

    Thanks. -Matt

    ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

    M 1 Reply Last reply
    0
    • P perlmunger

      I am using an ASP.NET 2.0 TreeView and was wanting to create different child TreeNode types for the different items that I am loading into the treeview. The idea behind this is that all I should have to do is check the node type when a node gets clicked and I can then respond accordingly. The problem, however, is that the child treenode I created that inherits from TreeNode gets reported to be a normal TreeNode type rather than the inherited treenode type when I use the typeof operator even though I instantiate it as the child TreeNode type. Am I missing something?

      class Child1TreeNode : TreeNode
      {
      }

      class Child2TreeNode : TreeNode
      {
      }

      And then in the Page Load:

      protected void Page_Load(object sender, EventArgs e)
      {
      if (!IsPostBack)
      {
      Child1TreeNode node = new Child1TreeNode();
      node.Text = "Hello World";
      node.Value = "1";
      Child2TreeNode node2 = new Child2TreeNode();
      node2.Text = "Goodbye World";
      node2.Value = "2";
      TreeNode topNode = TreeView.FindNode("Top");
      topNode.ChildNodes.Add( node );
      }
      }

      And then in the event handler for node changed:

      protected void TreeView_SelectedNodeChanged(object sender, EventArgs e)
      {
      TreeNode node = ((TreeView)sender).SelectedNode;
      if (node.GetType() == typeof(Child1TreeNode) )
      {
      // Do something related to Child1TreeNode types
      }
      else if (node.GetType() == typeof(Child2TreeNode) )
      {
      // Do something related to Child2TreeNode types
      }
      }

      Thanks. -Matt

      ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, What you are missing is that you also need to extend the TreeView control and override the CreateNode method. This method is basically used on postback to recreate the nodes tree, and by default it returns the TreeNode type and has no idea about your custom Child2TreeNode.

      P 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, What you are missing is that you also need to extend the TreeView control and override the CreateNode method. This method is basically used on postback to recreate the nodes tree, and by default it returns the TreeNode type and has no idea about your custom Child2TreeNode.

        P Offline
        P Offline
        perlmunger
        wrote on last edited by
        #3

        Web programming has all those little gotchas. I never seem to get over that. Anyhow, thanks for your help. Here's another question for you, though. How can I override the CreateNode method so that I can pass it an assortment of different objects that inherit from TreeNode and have them all retain their identity? Thanks again, -Matt

        ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

        M 1 Reply Last reply
        0
        • P perlmunger

          Web programming has all those little gotchas. I never seem to get over that. Anyhow, thanks for your help. Here's another question for you, though. How can I override the CreateNode method so that I can pass it an assortment of different objects that inherit from TreeNode and have them all retain their identity? Thanks again, -Matt

          ------------------------------------------ The 3 great virtues of a programmer: Laziness, Impatience, and Hubris. --Larry Wall

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Here you want to use multi different types of the tree node such as Child1TreeNode, Child2TreeNode, so you need a way to determine which object should be created when the treenode is rebuilt on postback. And the ViewState comes to mind as an option when you can use it to persist the node type in order to recreate the tree node. However, when you create a tree node in the overriden CreateNode method in the custom treeview, the ViewState of the tree node has not been loaded yet, so you will do that as soon as the ViewState is loaded. In the CreateNode, you simply return the base class of your two node types, say ChildTreeNode, and you will replace this base node with the exact type after the node type is determined. Below is the sample code to demonstrate this thing:

          public class ExTreeView : TreeView
          {
              protected override TreeNode CreateNode()
              {
                  return new ChildTreeNode(this);
              }
          }
                     
          public class ChildTreeNode : TreeNode
          {
              public ChildTreeNode() { }
                      
              public ChildTreeNode(string nodeType) { m\_NodeType = nodeType; }
                    
              public ChildTreeNode(ExTreeView owner) { this.m\_Owner = owner; }
                        
              private ExTreeView m\_Owner;
              internal ExTreeView Owner { get { return m\_Owner; } }
                     
              private string m\_NodeType;
              internal string NodeType
              {
                  get { return m\_NodeType; }
                  set { m\_NodeType = value; }
              }
                       
              protected override object SaveViewState()
              {
                  object baseState = base.SaveViewState();
                  return new Pair(baseState, NodeType);
              }
                     
              protected override void LoadViewState(object savedState)
              {
                  Pair p = savedState as Pair;
                  base.LoadViewState(p.First);
                  NodeType = p.Second as string;
                  
              //The node type is hard coded for the sake of simplicity.	       
                  TreeNode newNode;
                  if (NodeType == "Child1TreeNode") { newNode = new Child1TreeNode(); }
                  else if (NodeType == "Child2TreeNode") { newNode = new Child2TreeNode(); }
                  else { newNode = new TreeNode(); }
                      
                  newNode.Text = this.Text;
                  newNode.Value = this.Value;
                         
                  ReplaceCurrentNode(this, newNode);
              }
          
          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups