Binding Data to the TreeView Web Server Control

The ASP.NET TreeView control makes it easy to bind to a data source that supports the IHierarchicalDataSource interface, such as the XmlDataSource class and the SiteMapDataSource class. Additionally, you have complete control over which fields of the data source are used for the Text, Value, and other TreeNode properties.

For more information, see TreeView Web Server Control Overview.

The TreeView control supports declarative binding to an XML file by using XmlDataSource controls. You can bind a TreeView control to an XML file by creating an XmlDataSource control that represents the XML file and then assigning that XmlDataSource to your TreeView control. Furthermore, you can control how and what data is bound to the TreeView control by using several techniques, which include the following:

For more information about binding to an XML file, see Binding to Hierarchical Data. For a step-by-step example, see Walkthrough: Displaying Hierarchical Data in a TreeView Control.

The TreeView control supports declarative binding to a site-map file by using SiteMapDataSource controls. When binding to an instance of the SiteMapDataSource control, the Title and Url properties of each SiteMapNode are automatically associated with the Text and NavigateUrl properties of each TreeNode object in the TreeView control, so there is no need to create custom data bindings. For more information and an example of binding a TreeView control to a site map, see TreeView.

The populate-on-demand feature allows you to specify that a node not be populated with data when the TreeView control initially renders. Instead, the TreeNode control will request child-node data when a user clicks the node. When the PopulateOnDemand property of a TreeNode control is set to true, the TreeNodePopulate event occurs when a user clicks the node to expand it. You can create an event handler to retrieve data dynamically when the TreeNodePopulate event is raised, which allows tree data to be retrieved as needed. For more information, see Server Event Handling in ASP.NET Web Pages.

In addition to using a class that derives from DataSourceControl to declaratively bind data to the TreeView control, the TreeView control provides a DataSource property and a DataBind method for data binding.

Community Additions