How to use TreeView in asp.net

TreeView Server Control
TreeView is an asp.net web server control. TreeView displays hierarchical data such as a table of contents or files directory, in a tree structure. TreeView control is made up of nodes. We can display static data in a TreeView by creating a collection of TreeNode elements as children of the TreeView.

We can bind TreeView to a data source such as XmlDataSource and SiteMapDataSource. TreeView can also be bound to an XmlDocument object or a DataSet object.

TreeView node types are the parent node, child node, leaf node, and root node. The Parent node contains other nodes. child node is contained by another node and the leaf node have no children node. The root node is not contained by any other node.

Node has two properties Text and Value. text property value displayed in the browser and the value property is hidden in the browser and stores any additional data about the node. a node can have one mode selection mode or navigation mode. selection mode is the default. NavigateUrl property used in navigation mode.

We can customize the TreeView appearance. We can apply CSS or inline styles. TreeView can be designed by the following node styles HoverNodeStyle, LeafNodeStyle, NodeStyle, ParentNodeStyle, RootNodeStyle, and SelectedNodeStyle. TreeView also has basic design properties such as BackColor, ForeColor, BorderStyle, BorderColor, BorderWidth, Width, Height CssClass, EnableTheming, SkinID, ExpandDepth, ImageSet, etc.

We can customize images that are displayed in TreeView by using the following properties CollapseImageUrl, ExpandImageUrl, LineImagesFolder, and NoExpandImageUrl.

TreeView control provides many events such as TreeNodeCheckChanged, SelectedNodeChanged, TreeNodeExpanded, TreeNodeCollapsed, TreeNodePopulate, and TreeNodeDataBound.

When the user clicks a node, it can either raise a selection event via a postback. Or it goes to another page if the NavigateUrl property is set. If the NavigateUrl property is not set, then the node clicking raises the SelectedNodeChanged event.

The following example source codes describe you to learn more about TreeView control.
~/App_Data/ASPNETControls.xml

<?xml version="1.0" encoding="utf-8" ?>
<ToolBox>
  <Item Name="Standard Toolbox Controls">
    <Option Control="AdRotator" />
    <Option Control="BulletedList" />
    <Option Control="CheckBox" />
    <Option Control="FileUpload" />
    <Option Control="MultiView" />
  </Item>
  <Item Name="Data Toolbox Controls">
    <Option Control="ListView" />
    <Option Control="GridView" />
    <Option Control="LinqDataSource" />
    <Option Control="XmlDataSource" />
  </Item>
  <Item Name="Validation Toolbox Controls">
    <Option Control="CompareValidator" />
    <Option Control="CustomValidator" />
  </Item>
  <Item Name="Navigation Toolbox Controls">
    <Option Control="Menu" />
    <Option Control="SiteMapPath" />
    <Option Control="TreeView" />
  </Item>
</ToolBox>
Now create a Web Form name TreeView.aspx. Then add an XmlDataSource Control and a TreeView Control. For XmlDataSource Control's DataFile uses the AspNetControls.xml file and for TreeView Control's DataSourceID uses the XmlDataSource Control's ID. Then uses the asp:TreeNodeBinding to bind the XML file's data. Here is the source code of TreeView.aspx file.
TreeView.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to use TreeView in asp.net</title>
</head>
<body style="padding:25px">
    <form id="form1" runat="server">
    <div>
        <h2 style="color:MidnightBlue; font-style:italic;">      
            How to use TreeView
        </h2>      
        <hr width="450" align="left" color="Gainsboro" />
        <asp:XmlDataSource 
            ID="XmlDataSource1" 
            runat="server" 
            DataFile="~/App_Data/ASPNETControls.xml"
            >
        </asp:XmlDataSource>
        <asp:TreeView 
            ID="TreeView1" 
            runat="server" 
            DataSourceID="XmlDataSource1"
            >
            <DataBindings>
                <asp:TreeNodeBinding 
                    DataMember="ToolBox" 
                    Text="ASP.NET ToolBox"
                    />
                <asp:TreeNodeBinding 
                    DataMember="Item" 
                    TextField="Name"
                    />
                <asp:TreeNodeBinding 
                    DataMember="Option" 
                    TextField="Control"
                    />
            </DataBindings>
        </asp:TreeView>
    </div>
    </form>
</body>
</html>