TreeView Control in ASP.NET


The TreeView control is an object model in ASP.Net which allows creation of nodes dynamically. It will have the Root, Parent and Leaf.

The TreeView control has properties and events. It has some characteristics.

  • TreeView node can navigate to some other pages
  • The images and properties can be set to the nodes
  • It can have the lines, checkbox and images
  • It can set dynamically expand/collapse
  • The nodes can be created on demand

The sample treeview code.

<asp:TreeView ID="TreeView1" runat="server">
 <Nodes>
<asp:TreeNode Value="Child1" PopulateOnDemand="true" ShowCheckBox="true"  Expanded="True" Text="1">
        <asp:TreeNode Value="Grandchild1" Text="A" />
        <asp:TreeNode Value="Grandchild2" Text="B" />
        </asp:TreeNode>
        <asp:TreeNode Value="Child2" Text="2" />
        <asp:TreeNode Value="Child3" Expanded="True" Text="3">
        <asp:TreeNode Value="Grandchild1" Text="A" />
        </asp:TreeNode>
</Nodes>
</asp:TreeView>

There are some properties that are useful for defining the behavior of the TreeView. These properties are Boolean value based.

  1. PopulateOnDemand - It will populate the node under another node on the demand. It will help to increase the performance; it would not create and render at the time of initial loading.
  2. ShowCheckBox   - This property used to set the check-box is required or not.
  3. Expand - It accepts Boolean property to set the node has to be expanded or collapsed at the time of rendering.
  4. ShowLines - This is used to show the flow of the nodes in the lines.
Let's see an example of the SQL Server TreeView. It will show all the databases in the given SQL Server.

SqlTree1.jpg

The databases are the parent nodes. All the databases have the child nodes like Tables, Views, Stored procedures, Triggers, Functions.

SqlTree2.jpg

Every table in the database will have the list of tables underlying the database.

SqlTree3.jpg

Click of every table will show the schema of the table.

SqlTree4.jpg


Please download the code attachment, change your SQL Sserver connection string and check out the code.