Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7873313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:28:31+00:00 2026-06-03T02:28:31+00:00

I need to extend the TreeNode class such that I can add custom properties

  • 0

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:

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; }
}

If I create a CustomTreeNode and add it to a TreeView:

CustomTreeNode node = new CustomTreeNode(1, "CustomType");            
treeView.Nodes.Add(node);

I would then get a casting exception doing the following:

CustomTreeNode selectedNode = (CustomTreeNode)TreeView.SelectedNode;

because TreeView returns a TreeNode, not a CustomTreeNode.

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:

public class CustomTreeView : TreeView
{
    protected override TreeNode CreateNode()
    {
        return new CustomTreeNode();
    }
}

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.

Any help much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:28:32+00:00Added an answer on June 3, 2026 at 2:28 am

    This is what I came up with (experts, any advice welcomed). Instantiate the CustomTreeNodes in your code behind and set the properties via setters. Modify your CustomTreeNode class to persist the values in ViewState. The node returned by your custom tree view’s CreateNode will load the ViewState information.

    TreeNode class:

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CustomTreeNode runat=server></{0}:CustomTreeNode>")]
    public class CustomTreeNode : TreeNode
    {
        private const int NODE_TYPE = 1;
        private const int NODE_ID = 2;
    
        public string NodeType { get; set; }
        public int NodeId { get; set; }
    
        protected override void LoadViewState(Object savedState)
        {
            if (savedState != null)
            {
                object[] myState = (object[])savedState;
                if (myState[0] != null)
                    base.LoadViewState(myState[0]);
                if (myState[NODE_TYPE] != null)
                    this.NodeType = (string)myState[NODE_TYPE];
                if (myState[NODE_ID] != null)
                    this.NodeId = (int)myState[NODE_ID];
    
            }
        }
    
        protected override Object SaveViewState()
        {
            object baseState = base.SaveViewState();
            object[] allStates = new object[3];
            allStates[0] = baseState;
            allStates[NODE_TYPE] = this.NodeType;
            allStates[NODE_ID] = this.NodeId;
    
            return allStates;
        }
    }
    

    TreeView class:

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CustomTreeView runat=server></{0}:CustomTreeView>")]
    public class CustomTreeView : TreeView
    {
        protected override TreeNode CreateNode()
        {
            // Tree node will get its members populated with the data from VIEWSTATE
            return new CustomTreeNode();
        }
    }
    

    Simple .aspx file (Assuming that your custom control is defined in an assembly “Foo” and a namespace “Bar”:

    <%@ Register TagPrefix="customControl" Assembly="Foo" Namespace="Bar"  %>
    
    <customControl:CustomTreeView ID="sampleTree" 
        runat="server" onselectednodechanged="sampleTree_SelectedNodeChanged"></customControl:CustomTreeView>
    <asp:Label ID="lblSelectedNode" runat="server" ></asp:Label>
    

    CodeBehind:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateTree();
            }
        }
    
        private void PopulateTree()
        {
            sampleTree.Nodes.Clear();
            CustomTreeNode root = new CustomTreeNode();
            root.Value = "root node";
    
            sampleTree.Nodes.Add(root);
    
            // Creating some fake nodes (you would of course be using real data)
            for (int i = 0; i < 10; i++)
            {
                CustomTreeNode child = new CustomTreeNode();
                child.NodeId = i;               // Saved in ViewState
                child.NodeType = "Type " + i;   // Saved in ViewState
                child.Value = child.NodeType;
                root.ChildNodes.Add(child);
            }
        }
    
        protected void sampleTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            CustomTreeView cTreeView = (CustomTreeView) sender;
            lblSelectedNode.Text = ((CustomTreeNode)cTreeView.SelectedNode).NodeType;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I need to extend the CListControl class in C++/MFC, which will add several
I need to extend a class, which is encapsulated in a closure. This base
I have the capability to extend a class at compile time, but I need
I'd like to extend the WTP(e.g. custom personal wizard), I need to view the
I'm working on a project where I need to extend WebClient for a custom
I have a mission critical Perl-CGI server-side application that I need to extend or
I need to create a custom block in web config as follows: <MySettings> <add
This is an external class I need to extend: public class Binary { public
I'm new to js and need to extend dom Elements with my custom methods
I'm new to objective-c and need to extend a standard class of a framework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.