Article ID: 2527108
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.
Collapse this imageExpand this image
Download informationTo download this code sample, click one of the following links:
Technical overviewWhen 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 OverviewIn 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:
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:
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:
Add an object property that is named "Tag" in the CustomTreeNode class.
In the CustomTreeNode class, override the SaveViewState() method to save property values to the view state.
In the CustomTreeNode class, override the LoadViewState(object) method to restore the property values from the view state.
In the CustomTreeView class, override the CreateNode() method to create the CustomTreeNode object as a tree node in the postback request.
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:
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:
Additionally, run the following code to retrieve the entity from the custom TreeView Control:
LanguagesThis code sample contains the following programming languages:
Collapse this tableExpand this table
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 samplesTo 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:
All-In-One Code Framework samples
For more information about the TreeNode.Tag property, visit the following MSDN website:
General information about the TreeNode.Tag propertyFor more information about the Control.SaveViewState method, visit the following MSDN website:
General information about the Control.SaveViewState methodFor more information about the Control.LoadViewState method, visit the following Microsoft website:
General information about Control.LoadViewState MethodFor more information about the TreeView.CreateNode method, visit the following Microsoft website:
General information about the TreeView.CreateNode method
Rapid publishing disclaimerMicrosoft 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.
(http://go.microsoft.com/fwlink/?LinkId=151500)for other considerations.
Contact us for more help
Connect with Answer Desk for expert help.