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];
base.LoadViewState(arrState[1]);
}
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:
[Serializable]
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();
CustomTreeView1.Nodes.Add(treeNode);
}
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
Languages
This code sample contains the following programming languages:
Language | Project Name |
---|---|
Visual C# | CSASPNETInheritingFromTreeNode |
Visual Basic.NET | VBASPNETInheritingFromTreeNode |