How to extend a built-in TreeNode class of the ASP.NET TreeView control


This article describes an All-In-One Code Framework sample that is available for download. This code sample demonstrates how to extend a built-in TreeNode class of the ASP.NET TreeView control.In the TreeView windows forms, each TreeNode class has a "Tag" property that can be used to store a custom object. This code sample creates the CustomTreeView custom TreeView control to implement the same feature in the ASP.NET TreeView control.

Difficulty level

Download information

To download this code sample, click one of the following links:

Technical overview

When you create a new class that is inherited from the TreeNode class and use the object of the new class in the TreeView control, the property values of the TreeNode class is not remained in the postback request. Because HTTP protocol is stateless, the ASP.NET runtime does not save these properties automatically across postback requests.

To save the properties of the TreeNode class, you can override the SaveViewState() and LoadViewState(object) methods so that the ASP.NET runtime saves and the properties to the view state via code.

Additionally, a TreeView object is created by the ASP.NET runtime in the postback request. By default, the TreeView class calls the CreateNode() method to create a default TreeNode object and generates tree nodes. You can also override the CreateNode() method to create custom TreeNode object.

Sample Overview

In this code sample, two methods are overridden to save the values of the "Tag" property to the view state and restore the values of the "Tag" property from the view state. To make the postback request creating the custom TreeNode class, you can create a new custom server control that is inherited from the TreeView control and override the CreateNode() method.To do this, follow these steps:

Step 1

Add a new class file that is named "CustomTreeView" to a project. Add the following code at the beginning of the file to import the System.Web.UI.WebControls namespace which contains a built-in ASP.NET TreeView control:
using System.Web.UI.WebControls;

Step 2

In the CustomTreeView class file, create the "CustomTreeNode" class that is inherited from the TreeNode class. Run the following code to implement two constructors immediately:
public CustomTreeNode() : base()

public CustomTreeNode(TreeView owner, bool isRoot) : base(owner, isRoot)

Step 3

Add an object property that is named "Tag" in the CustomTreeNode class.
/// <summary>
/// Gets or sets the object that contains data about the tree node.
/// </summary>
public object Tag { get; set; }

Step 4

In the CustomTreeNode class, override the SaveViewState() method to save property values to the view state.
protected override object SaveViewState()
object[] arrState = new object[2];
arrState[1] = base.SaveViewState();
arrState[0] = this.Tag;

return arrState;
Step 5

In the CustomTreeNode class, override the LoadViewState(object) method to restore the property values from the view state.
protected override void LoadViewState(object state)
object[] arrState = state as object[];

this.Tag = arrState[0];
Step 6

In the CustomTreeView class, override the CreateNode() method to create the CustomTreeNode object as a tree node in the postback request.
public class CustomTreeView : TreeView
/// <summary>
/// Returns a new instance of the TreeNode class.
/// The CreateNode is a helper method.
/// </summary>
protected override TreeNode CreateNode()
return new CustomTreeNode(this, false);
Note To use the custom TreeView control in the ASP.NET pages, perform the same operation as using built-in TreeView control. However, the custom TreeView control supports a new feature that can be used to store an object to the Tag property of the tree node and the object values will be retained in the postback requests.
For example, we have a class that defines an entity as follows:
public class MyItem
public string Title { get; set; }
After add a custom TreeView control that is named "CustomTreeView1" in the page, you can run the following code to store an entity into a tree node:
// Display 10 nodes in the TreeView control.
for (int i = 0; i < 10; i++)
CustomTreeNode treeNode = new CustomTreeNode();

// Assign a custom object to the tree node.
MyItem item = new MyItem();
item.Title = "Object " + i.ToString();
treeNode.Tag = item;

treeNode.Value = i.ToString();
treeNode.Text = "Node " + i.ToString();

Additionally, run the following code to retrieve the entity from the custom TreeView Control:
CustomTreeNode treeNode = (CustomTreeNode)CustomTreeView1.SelectedNode;
MyItem item = (MyItem)treeNode.Tag;

Technology category

  • ASP.NET 2.0
  • ASP.NET 3.5
  • ASP.NET 4.0


This code sample contains the following programming languages:
LanguageProject Name
Visual C#CSASPNETInheritingFromTreeNode
Visual Basic.NETVBASPNETInheritingFromTreeNode

More Information

What is All-In-One Code Framework?

All-In-One Code Framework shows most Microsoft development techniques by using code samples in different programming languages. Each example is carefully selected, composed, and documented to show one common code scenario. For more information about All-In-One Code Framework, visit the following Microsoft website:

How to find more All-In-One Code Framework samples

To find more All-In-One Code Framework samples, search for "kbcodefx" together with related keywords on the Microsoft support Web site. Or, visit the following Microsoft website:


For more information about the TreeNode.Tag property, visit the following MSDN website: For more information about the Control.SaveViewState method, visit the following MSDN website: For more information about the Control.LoadViewState method, visit the following Microsoft website:For more information about the TreeView.CreateNode method, visit the following Microsoft website:
Rapid publishing disclaimer
Microsoft corporation and/or its respective suppliers make no representations about the suitability, reliability, or accuracy of the information and related graphics contained herein. All such information and related graphics are provided "as is" without warranty of any kind. Microsoft and/or its respective suppliers hereby disclaim all warranties and conditions with regard to this information and related graphics, including all implied warranties and conditions of merchantability, fitness for a particular purpose, workmanlike effort, title and non-infringement. You specifically agree that in no event shall Microsoft and/or its suppliers be liable for any direct, indirect, punitive, incidental, special, consequential damages or any damages whatsoever including, without limitation, damages for loss of use, data or profits, arising out of or in any way connected with the use of or inability to use the information and related graphics contained herein, whether based on contract, tort, negligence, strict liability or otherwise, even if Microsoft or any of its suppliers has been advised of the possibility of damages.


Article ID: 2527108 - Last Review: Apr 7, 2011 - Revision: 1