In this article you will how to create a TreeView Control in ASP.NET and how to use the properties of a TreeView Control.
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.
Open Microsoft Visual Studio then select "File" -> "New" -> "Project..." (Ctrl+Shift+N). Step 2
Add an ASP.NET Web Forms Application and give it the name TreeViewControl. Step 3
Open Solution Explorer then add a Webform and give it a name such as "TreeView.aspx". Step 4
Now drag and drop a TreeViewControl from the ToolBox. And after adding a TreeView you will see the following code:
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:
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. 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: Step 2
Now write the following code and see the output when clicking on the Employee link on the TreeView.
Step 3
Add a Webform and give it a name such as Employer as in the following: Step 4
Now write the following code and see the output when the Employer link of the TreeView is clicked.
Add a Webform and give it a name such as Admin. Step 6
Now write the following code and see the output when the Admin link of the TreeView is clicked.
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. Now see the output: 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. 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. Now after applying the styles of the TreeView the final code will be,
Output of the 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. Step 2Use the Autoformate Property and apply the Arrows2 format and see the output. This is about the TreeView control in ASP.NET.
ASP.NET MVC 5: A Beginner’s Guide