System.Web.UI.WebControls Namespace
.NET Framework version 2.0
Displays hierarchical data, such as a table of contents, in a tree structure.
Visibility |
Constructor |
Parameters |
public |
TreeView |
( )
|
Visibility |
Name |
Value Type |
Accessibility |
public |
AutoGenerateDataBindings
|
Boolean |
[ Get , Set ] |
public |
CheckedNodes
|
TreeNodeCollection |
[ Get ] |
public |
CollapseImageToolTip
|
String |
[ Get , Set ] |
public |
CollapseImageUrl
|
String |
[ Get , Set ] |
public |
DataBindings
|
TreeNodeBindingCollection |
[ Get ] |
public |
EnableClientScript
|
Boolean |
[ Get , Set ] |
public |
ExpandDepth
|
Int32 |
[ Get , Set ] |
public |
ExpandImageToolTip
|
String |
[ Get , Set ] |
public |
ExpandImageUrl
|
String |
[ Get , Set ] |
public |
HoverNodeStyle
|
Style |
[ Get ] |
public |
ImageSet
|
TreeViewImageSet |
[ Get , Set ] |
public |
LeafNodeStyle
|
TreeNodeStyle |
[ Get ] |
public |
LevelStyles
|
TreeNodeStyleCollection |
[ Get ] |
public |
LineImagesFolder
|
String |
[ Get , Set ] |
public |
MaxDataBindDepth
|
Int32 |
[ Get , Set ] |
public |
NodeIndent
|
Int32 |
[ Get , Set ] |
public |
Nodes
|
TreeNodeCollection |
[ Get ] |
public |
NodeStyle
|
TreeNodeStyle |
[ Get ] |
public |
NodeWrap
|
Boolean |
[ Get , Set ] |
public |
NoExpandImageUrl
|
String |
[ Get , Set ] |
public |
ParentNodeStyle
|
TreeNodeStyle |
[ Get ] |
public |
PathSeparator
|
Char |
[ Get , Set ] |
public |
PopulateNodesFromClient
|
Boolean |
[ Get , Set ] |
public |
RootNodeStyle
|
TreeNodeStyle |
[ Get ] |
public |
SelectedNode
|
TreeNode |
[ Get ] |
public |
SelectedNodeStyle
|
TreeNodeStyle |
[ Get ] |
public |
SelectedValue
|
String |
[ Get ] |
public |
ShowCheckBoxes
|
TreeNodeTypes |
[ Get , Set ] |
public |
ShowExpandCollapse
|
Boolean |
[ Get , Set ] |
public |
ShowLines
|
Boolean |
[ Get , Set ] |
public |
SkipLinkText
|
String |
[ Get , Set ] |
public |
Target
|
String |
[ Get , Set ] |
public |
Visible
|
Boolean |
[ Get , Set ] |
|
The TreeView control is used to display hierarchical data, such as a table of contents or file directory, in a tree structure and supports the following features:
- Data binding that allows the nodes of the control to be bound to XML, tabular, or relational data.
- Site navigation through integration with the SiteMapDataSource control.
- Node text that can be displayed as either plain text or hyperlinks.
- Programmatic access to the TreeView object model to create trees, populate nodes, set properties, and so on dynamically.
- Client-side node population ( on supported browsers ).
- The ability to display a check box next to each node.
- Customizable appearance through themes, user-defined images, and styles.
The TreeView control is made up of nodes. Each entry in the tree is called a node and is represented by a TreeNode object. Node types are defined as follows:
- A node that contains other nodes is called a parent node.
- The node that is contained by another node is called a child node.
- A node that has no children is called a leaf node.
- The node that is not contained by any other node but is the ancestor to all the other nodes is the root node.
A node can be both a parent and a child, but root, parent, and leaf nodes are mutually exclusive. Several visual and behavioral properties of nodes are determined by whether a node is a root, parent, or leaf node.
Although a typical tree structure has only one root node, the TreeView control allows you to add multiple root nodes to your tree structure. This is useful when you want to display item listings without displaying a single root node, as in a list of product categories.
Each node has a Text property and a Value property. The value of the Text property is displayed in the TreeView, while the Value property is used to store any additional data about the node, such as data that is passed to the postback event that is associated with the node.
A node can be in one of two modes: selection mode and navigation mode. By default, a node is in selection mode. To put a node into navigation mode, set the NavigateUrl property for the node to a value other than an empty string ( "" ). To put a node into selection mode, set the NavigateUrl property for the node to an empty string ( "" ).
The simplest data model of the TreeView control is static data. To display static data using declarative syntax, first nest opening and closing <Nodes> tags between the opening and closing tags of the TreeView control. Next, create the tree structure by nesting <asp:TreeNode> elements between the opening and closing <Nodes> tags. Each <asp:TreeNode> element represents a node in the tree and maps to a TreeNode object. You can set the properties of each node by setting the attributes of its <asp:TreeNode> element. To create child nodes, nest additional <asp:TreeNode> elements between the opening and closing <asp:TreeNode> tags of the parent node.
The TreeView control can also be bound to data. You can use either of two methods to bind the TreeView control to the appropriate data source type:
- The TreeView control can use any data source control that implements the IHierarchicalDataSource interface, such as an XmlDataSource control or a SiteMapDataSource control. To bind to a data source control, set the DataSourceID property of the TreeView control to the ID value of the data source control. The TreeView control automatically binds to the specified data source control. This is the preferred method to bind to data.
- The TreeView control can also be bound to an XmlDocument object or a DataSet object with relations. To bind to one of these data sources, set the DataSource property of the TreeView control to the data source, and then call the DataBind method.
When binding to a data source where each data item contains multiple properties ( such as an XML element with several attributes ), a node displays the value that is returned by the ToString method of the data item, by default. In the case of an XML element, the node displays the element name, which shows the underlying structure of the tree but is not very useful otherwise. You can bind a node to a specific data item property by specifying tree node bindings using the DataBindings collection. The DataBindings collection contains TreeNodeBinding objects that define the relationship between a data item and the node that it is binding to. You can specify the criteria for binding and the data item property to display in the node. For more information on tree node bindings, see TreeNodeBinding.
NOTE: A malicious user can create a callback request and get data for the nodes of the TreeView control that the page developer is not displaying. Therefore, security of the data must be implemented by the data source. Do not use the MaxDataBindDepth property to hide data.
There are cases where it is not practical to statically define the tree structure because the data source returns too much data or because the data to display depends on information that you get at run time. For these cases, the TreeView control supports dynamic node population. When the PopulateOnDemand property for a node is set to true, that node gets populated at run time when the node is expanded. To populate a node dynamically, you must define an event-handling method that contains the logic to populate a node for the TreeNodePopulate event.
Microsoft Internet Explorer version 5.0 and later and Netscape 6.0 and later can also take advantage of client-side node population. Client-side node population enables the TreeView control to populate a node using client script when users expand the node, without requiring a round trip to the server. For more information on client-side node population, see PopulateNodesFromClient.
For examples illustrating use of this control, see the individual member types of this class. For syntax information, see TreeView in ASP.NET Syntax for Web Controls.
ASP.NET Navigation Controls TreeView Web Server Control