The TreeView Web server control can display various types of data: static data specified by the control in the declaration mode; Data bound to the control; or dynamically add data to the TreeView control in response to user operations.
1. Display static data
The simplest data architecture is declarative static data. To use declarative syntax to display static data, create a set of child nodes of the TreeView control.
The following example shows how to display the TreeView control that contains three nodes (two of them have subnodes.
<Asp: TreeView ID = "TreeView1" Runat = "server">
<Nodes>
<Asp: TreeNode Value = "Parent1" Expanded = "True" Text = "1">
<Asp: TreeNode Value = "Child1A" Text = "A"/>
<Asp: TreeNode Value = "Child1B" Text = "B"/>
</Asp: TreeNode>
<Asp: TreeNode Value = "Parent2" Text = "2">
</Asp: TreeNode>
<Asp: TreeNode Value = "Parent3" Expanded = "True" Text = "3">
<Asp: TreeNode Value = "Child3A" Text = "A">
</Asp: TreeNode>
</Asp: TreeNode>
</Nodes>
</Asp: TreeView>
2. bind to the data source
To display the data bound to the control as declared, first add a hierarchical data source control (such as the xmlDataSource Control) to the page and assign an ID to the control. Then, set the performanceid attribute of the TreeView control to the ID of the data source control. The TreeView control automatically binds to the data source and displays the value of the data source.
Note: The TreeView control can be bound to any data source control that implements the IHierarchicalDataSource interface, such as the SiteMapDataSource object or XmlDataSource object.
By default, when a data source is bound, if each data item of the data source contains multiple attributes (for example, an XML element has multiple attributes ), the node displays the value returned by the ToString method of the data item. For an XML element, the node displays the element name. This architecture shows the basic structure of the tree, but it is not very useful in other aspects. You can use the DataBindings set to specify the tree node to bind the node to a specific data item attribute. The DataBindings set contains a TreeNodeBinding object that defines the relationship between a data item and the node to which it is bound. You can specify the binding conditions and attributes of the data items to be displayed on the node.
Note: The TreeView control also provides a DataSource attribute and a DataBind Method for manual data binding.
3. dynamically display data
The data structure may not be defined statically, or the data may depend on the information collected during the runtime. You can programmatically populate the TreeNode object with the TreeView control's Nodes set in the server code, or use the PopulateOnDemand function of the TreeView control to dynamically fill the node when the parent node on the client is expanded.