Select the product you need help with
How to extend a built-in TreeNode class of the ASP.NET TreeView controlArticle ID: 2527108 On This PageINTRODUCTIONThis 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 levelCollapse 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: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: 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: Step 3 Add an object property that is named "Tag" in the CustomTreeNode class. Step 4 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. For example, we have a class that defines an entity as follows: Technology category
LanguagesThis code sample contains the following programming languages:Collapse this table
MORE INFORMATIONWhat 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:http://1code.codeplex.com
(http://1code.codeplex.com/)
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
(http://support.microsoft.com/search/default.aspx?query=kbcodefx)
REFERENCESFor more information about the TreeNode.Tag property, visit the following MSDN website: General information about the TreeNode.Tag property For more information about the Control.SaveViewState method, visit the following MSDN website:
(http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.tag.aspx)
General information about the Control.SaveViewState method For more information about the Control.LoadViewState method, visit the following Microsoft website:
(http://msdn.microsoft.com/en-us/library/system.web.ui.control.saveviewstate.aspx)
General information about Control.LoadViewState Method For more information about the TreeView.CreateNode method, visit the following Microsoft website:
(http://msdn.microsoft.com/en-us/library/system.web.ui.control.loadviewstate.aspx)
General information about the TreeView.CreateNode method
(http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.createnode.aspx)
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.Note This is a "FAST PUBLISH" article created directly from within the Microsoft support organization. The information contained herein is provided as-is in response to emerging issues. As a result of the speed in making it available, the materials may include typographical errors and may be revised at any time without notice. See Terms of Use
(http://go.microsoft.com/fwlink/?LinkId=151500)
for other considerations.PropertiesArticle ID: 2527108 - Last Review: April 7, 2011 - Revision: 1.0
|





Back to the top








