TreeView Control In ASP.NET

Introduction


In this article you will how to create a TreeView Control in ASP.NET and how to use the properties of a TreeView Control.

TreeView Control


The TreeView Web control is useful to display hierarchical data in a tree structure. A TreeView is a collection of TreeNode objects.

The contents of the TreeView control can be specified directly in the control or bound to,

  1. XML file
  2. web.sitemap file
  3. Database table

Now, I am explaining how to create a TreeView control in ASP.NET with the contents for the TreeView control specified directly in the control and by using various properties how to apply formating to the TreeView control.

Procedure to create a TreeView Control in ASP.NET


Step 1

Open Microsoft Visual Studio then select "File" -> "New" -> "Project..." (Ctrl+Shift+N).

create new project

Step 2

Add an ASP.NET Web Forms Application and give it the name TreeViewControl.

web form

Step 3

Open Solution Explorer then add a Webform and give it a name such as "TreeView.aspx".

add new item

item name

Step 4

Now drag and drop a TreeViewControl from the ToolBox.

navigation

And after adding a TreeView you will see the following code:

  1.   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeView.aspx.cs" Inherits="TreeViewControl.TreeView" %>    
  2.       
  3.   <!DOCTYPE html>    
  4.       
  5.   <html xmlns="http://www.w3.org/1999/xhtml">    
  6.   <head runat="server">    
  7.       <title></title>    
  8.   </head>    
  9.       
  10.  <body>    
  11.      <form id="form1" runat="server">    
  12.          <div style="font-family: Arial">    
  13.              <asp:TreeView runat="server" ID="TreeView1">    
  14.              </asp:TreeView>    
  15.          </div>    
  16.      </form>    
  17.  </body>    
  18.  </html>    

Step 5

Write the following code inside the <Nodes></Nodes> element and create Nodes of the TreeView in the "TreeView.aspx" page as in the following:

  1.             <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeView.aspx.cs" Inherits="TreeViewControl.TreeView" %>    
  2.       
  3.   <!DOCTYPE html>    
  4.       
  5.   <html xmlns="http://www.w3.org/1999/xhtml">    
  6.   <head runat="server">    
  7.       <title></title>    
  8.   </head>    
  9.       
  10.  <body>    
  11.      <form id="form1" runat="server">    
  12.         <div style="font-family: Arial">    
  13.              <asp:TreeView runat="server" ID="TreeView1">    
  14.                  <Nodes>    
  15.                    <asp:TreeNode Text="Home" NavigateUrl="~/Home.aspx" Target="_blank"/>    
  16.                     <asp:TreeNode Text="Employee" NavigateUrl="~/Employee.aspx" Target="_blank">    
  17.                       <asp:TreeNode Text="Upload Resume" NavigateUrl="~/Upload_Resume.aspx" Target="_blank" />    
  18.                       <asp:TreeNode Text="Edit Resume" NavigateUrl="~/Edit_Resume.aspx" Target="_blank" />    
  19.                       <asp:TreeNode Text="View Resume" NavigateUrl="~/View_Resume.aspx" Target="_blank" />    
  20.                    </asp:TreeNode>    
  21.                     <asp:TreeNode Text="Employer" NavigateUrl="~/Employer.aspx" Target="_blank">    
  22.                         <asp:TreeNode Text="Upload Job" NavigateUrl="~/Upload_Job.aspx" Target="_blank" />    
  23.                         <asp:TreeNode Text="Edit Job" NavigateUrl="~/Edit_Job.aspx" Target="_blank" />    
  24.                         <asp:TreeNode Text="View Job" NavigateUrl="~/View_Job.aspx" Target="_blank" />    
  25.                     </asp:TreeNode>    
  26.                     <asp:TreeNode Text="Admin" NavigateUrl="~/Admin.aspx" Target="_blank">    
  27.                        <asp:TreeNode Text="Add User" NavigateUrl="~/Add_User.aspx" Target="_blank" />    
  28.                        <asp:TreeNode Text="Edit User" NavigateUrl="~/Edit_Use.aspx" Target="_blank" />    
  29.                        <asp:TreeNode Text="View User" NavigateUrl="~/View_User.aspx" Target="_blank" />    
  30.                     </asp:TreeNode>    
  31.                  </Nodes>    
  32.              </asp:TreeView>    
  33.          </div>    
  34.      </form>    
  35.  </body>    
  36.  </html>    
Now run the code and see the following output:

run page

Now we can expand and colapse the TreeView like the following image.

TreeView

Now before going further we add one new webform and specify the name as "Home.aspx" for the Home link of the TreeView.

add web form

aspx page

Write the following code for the Home Page at Home.aspx.
  1.   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="TreeViewControl.Home" %>    
  2.       
  3.   <!DOCTYPE html>    
  4.       
  5.      <html xmlns="http://www.w3.org/1999/xhtml">    
  6.      <head runat="server">    
  7.          <title></title>    
  8.      </head>    
  9.      <body>    
  10.         <form id="form1" runat="server">    
  11.         <div>    
  12.             <asp:Label ID="HomePage" runat="server" Text="Wellcome To Home Page" BackColor="Yellow" ForeColor="#000099"></asp:Label>    
  13.         </div>    
  14.         </form>    
  15.     </body>    
  16.  </html>    
Now see the output:

welcome to home page

Now you can see that the TreeNode object has several attributes.
  1. <asp:TreeNode Text="Home" NavigateUrl="~/Home.aspx" Target="_blank"/>   

Now notice that we have specified the Text attribute to display TreeView text.

The NavigateUrl attribute is the page to which you will navigate to when you click on the link on the TreeView.

We specified the Target attribute by which, when we click on the Home link, it is opening the Home Page but in a new window it is possible because we are using the Target attribute.

Target attribute

So finally we added content to the TreeView Control directly inside the control itself.

Now again use the following procedure and create more pages for Employee, Employer and Admin.

Step 1

Add a Webform and give it a name such as Employee:

add new web form

enter name

Step 2

Now write the following code and see the output when clicking on the Employee link on the TreeView.

  1.   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employee.aspx.cs" Inherits="TreeViewControl.Employee" %>    
  2.       
  3.      <!DOCTYPE html>    
  4.       
  5.         <html xmlns="http://www.w3.org/1999/xhtml">    
  6.         <head runat="server">    
  7.       <title></title>    
  8.         </head>    
  9.         <body>    
  10.            <form id="form1" runat="server">    
  11.         <div>    
  12.          <asp:Label ID="EmployeePage" runat="server" Text="Wellcome To Employee Page" BackColor="Pink" ForeColor="#000099"></asp:Label>    
  13.         </div>    
  14.         </form>    
  15.     </body>    
  16.  </html>    

wellcome to employee page

Step 3

Add a Webform and give it a name such as Employer as in the following:

add another web form

specify name for item

Step 4

Now write the following code and see the output when the Employer link of the TreeView is clicked.

  1.   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Employer.aspx.cs" Inherits="TreeViewControl.Employer" %>    
  2.       
  3.   <!DOCTYPE html>    
  4.       
  5.   <html xmlns="http://www.w3.org/1999/xhtml">    
  6.   <head runat="server">    
  7.       <title></title>    
  8.   </head>    
  9.   <body>    
  10.      <form id="form1" runat="server">    
  11.      <div>    
  12.      <asp:Label ID="EmployerPage" runat="server" Text="Wellcome To Employer Page" BackColor="Green" ForeColor="#000099"></asp:Label>    
  13.      </div>    
  14.      </form>    
  15.  </body>    

employee page

Step 5

Add a Webform and give it a name such as Admin.

add another form

give name as Admin

Step 6

Now write the following code and see the output when the Admin link of the TreeView is clicked.

  1.   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Admin.aspx.cs" Inherits="TreeViewControl.Admin" %>    
  2.       
  3.   <!DOCTYPE html>    
  4.       
  5.   <html xmlns="http://www.w3.org/1999/xhtml">    
  6.   <head runat="server">    
  7.       <title></title>    
  8.   </head>    
  9.   <body>    
  10.      <form id="form1" runat="server">    
  11.      <div>    
  12.      <asp:Label ID="AdminPage" runat="server" Text="Wellcome To Admin Page"  ForeColor="#000099" BackColor="#00CCFF"></asp:Label>    
  13.      </div>    
  14.      </form>    
  15.  </body>    
  16.  </html>    
welcome to admin page

Similarly we can create all the pages that we are using in the TreeView link.

To configure the look and feel of the TreeView control, the following styles can be used. 
  1. HoverNodeStyle
  2. LeafNodeStyle
  3. RootNodeStyle
  4. SelectedNodeStyle and so on.

Now we will see how to apply these styles for the TreeView.

Here we want that when we mouseover on links of the TreeView the background color of the nodes will change and the font will also change to bold.

Step 1

Go to the design part of the Webform then in the Open properties of the TreeView set the following properties.

properties of the TreeView

font and style

Now see the output:

mouse over head

Step 2

Now we want to apply LeafNodeStyle, LeafNodes are that have no childrens.

So go to the properties and set the background color to Orange of LeafNodes.

set background color

Step 3

Now we want to apply a RootNodeStyle. On the properties window specify the background color Green of RootNodes and after running the project see the following output of the TreeView.

output of the TreeView

RootNodeStyle

Now after applying the styles of the TreeView the final code will be,

  1.   <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeView.aspx.cs" Inherits="TreeViewControl.TreeView" %>    
  2.       
  3.   <!DOCTYPE html>    
  4.       
  5.   <html xmlns="http://www.w3.org/1999/xhtml">    
  6.   <head runat="server">    
  7.       <title></title>    
  8.   </head>    
  9.   <body>    
  10.      <form id="form1" runat="server">    
  11.          <div style="font-family: Arial">    
  12.              <asp:TreeView runat="server" ID="TreeView1">    
  13.                  <HoverNodeStyle BackColor="#00CCFF" Font-Bold="True" />    
  14.                  <LeafNodeStyle BackColor="#FF6600" />    
  15.                  <Nodes>    
  16.                      <asp:TreeNode Text="Home" NavigateUrl="~/Home.aspx" Target="_blank" />    
  17.                      <asp:TreeNode Text="Employee" NavigateUrl="~/Employee.aspx" Target="_blank">    
  18.                          <asp:TreeNode Text="Upload Resume" NavigateUrl="~/Upload_Resume.aspx" Target="_blank" />    
  19.                          <asp:TreeNode Text="Edit Resume" NavigateUrl="~/Edit_Resume.aspx" Target="_blank" />    
  20.                          <asp:TreeNode Text="View Resume" NavigateUrl="~/View_Resume.aspx" Target="_blank" />    
  21.                      </asp:TreeNode>    
  22.                      <asp:TreeNode Text="Employer" NavigateUrl="~/Employer.aspx" Target="_blank">    
  23.                          <asp:TreeNode Text="Upload Job" NavigateUrl="~/Upload_Job.aspx" Target="_blank" />    
  24.                          <asp:TreeNode Text="Edit Job" NavigateUrl="~/Edit_Job.aspx" Target="_blank" />    
  25.                          <asp:TreeNode Text="View Job" NavigateUrl="~/View_Job.aspx" Target="_blank" />    
  26.                      </asp:TreeNode>    
  27.                      <asp:TreeNode Text="Admin" NavigateUrl="~/Admin.aspx" Target="_blank">    
  28.                          <asp:TreeNode Text="Add User" NavigateUrl="~/Add_User.aspx" Target="_blank" />    
  29.                          <asp:TreeNode Text="Edit User" NavigateUrl="~/Edit_Use.aspx" Target="_blank" />    
  30.                          <asp:TreeNode Text="View User" NavigateUrl="~/View_User.aspx" Target="_blank" />    
  31.                      </asp:TreeNode>    
  32.                  </Nodes>    
  33.                  <RootNodeStyle BackColor="#009900" />    
  34.              </asp:TreeView>    
  35.          </div>    
  36.      </form>    
  37.  </body>    

Output of the final TreeView,

Final TreeView

Now If we want to apply formatting to a TreeView then we can use the Autoformate property and apply various formatting.

Step 1

Use the Autoformate Property and apply a Windows Help format and see the output.

Autoformat

page output

Step 2

Use the Autoformate Property and apply the Arrows2 format and see the output.

Arrows2 format

Arrows2 format output

This is about the TreeView control in ASP.NET.


Similar Articles