asp.net.ph

Skip Navigation Links

Adding TreeView Nodes Programmatically

Controls You Can Use on Web Forms   ASP.NET Navigation Controls   TreeView Control


The TreeView control exposes a Nodes collection, which comprise the root TreeNode objects defined for the control.

In the same manner, each TreeNode object exposes a ChildNodes collection, which comprise the child TreeNode objects, if any, defined for that particular node.

Root treeview nodes can be configured programmatically, by creating new instances of the TreeNode class and adding them to the Nodes collection in the order that they will appear in the treeview structure.

Likewise, child treeview nodes can be configured programmatically, by creating new instances of the TreeNode class and adding them to the ChildNodes collection of the parent node, in the order that they will appear in the treeview structure.

To display data dynamically, the TreeView control supports dynamic node population. When the TreeView control is configured to populate on demand ( the PopulateOnDemand property for a node is set to true ), the control raises a TreeNodePopulate event when the node is expanded, to which you can respond to accordingly.

To dynamically populate nodes on demand

  1. Set the PopulateOnDemand property of a node to true
  2. Define a method to handle the TreeNodePopulate event.

The method assigned to handle the event should be able to do the following:

  1. retrieve node data from a data source
  2. for each row of data in the source, create a TreeNode object
  3. bind the data into the node object
  4. set the node’s properties
  5. add the node structure to the ChildNodes collection of the node being populated

The following examples demonstrate how to populate the nodes of a TreeView control dynamically.

Populating TreeView Nodes On Demand
Run Sample | View Source
Populating TreeView Nodes On Demand Using Page Titles
Run Sample | View Source
Populating TreeView Nodes from a Database
Run Sample | View Source
See Also

Binding a TreeView Control to a Data Source   Customizing the TreeView Control User Interface



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note