ARTICLE

Using TreeView Control in ASP.NET

Posted by Sapna Articles | ASP.NET using VB.NET January 06, 2011
The ASP.NET TreeView control is used to populate TreeNodes on demand with child-nodes when expanding a TreeNode.
 
Reader Level:

The ASP.NET TreeView control is used to populate TreeNodes on demand with child-nodes when expanding a TreeNode. This behaviour can be used to increase the performance when loading large amount of data to a TreeView. Lets clear it more through an example:

ASP.NET TreeView Control

Create an ASP.NET webite and drag a TreeView-control from the Toolbox onto the page Default.aspx. After that we create some sample TreeNodes for a company's staff and assign the AutoFormat contacts to the TreeView:

TreeViewTask.gif

Here is the code snippets for the page Default.aspx.

<%@ Page Language="vb" Culture="vi-VN" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<
script runat="server">
     Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As EventArgs)
          lblMessage.Text = TreeView1.SelectedValue
     End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
      <title>TreeView Value</title>
</head>
<body style="height: 212px">
<asp:TreeView
   id="TreeView1"
   OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"
   Runat="server" Height="284px" Width="303px" ImageSet="Contacts" NodeIndent="10">
  <HoverNodeStyle Font-Underline="False"/>
     <Nodes>
         <asp:TreeNode
                 Text="HR Deptarment"
                 Value="HR Deptarment">
               <asp:TreeNode
                     Text="Director">
                    <asp:TreeNode
                          Text="Mr. Sunil Kumar"
                          Value="Mr. Sunil Kumar" />
                    </asp:TreeNode>
               <asp:TreeNode
                     Text="Managers">
                    <asp:TreeNode
                          Text="Project Manager">
                        <asp:TreeNode
                              Value="Mr. Keshav Singh"
/>
                        <asp:TreeNode
                              Text="Mr. Vinod Kashyap"
                              Value="Mr. Vinod Kashyap" />
                   <asp:TreeNode
                         Text="Sales Manager">
                        <asp:TreeNode
                              Text="Mr. Rahul Gupta"
                              Value="Mr. Rahul Gupta" />
                        <asp:TreeNode          
                              Text
="Mr. Aditya Pathak"
                              Value="Mr. Aditya Pathak" />
                   </asp:TreeNode>
        </asp:TreeNode
>
     </Nodes>
<NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="blue" 
  HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
<ParentNodeStyle Font-Bold="True" ForeColor="black" />
<SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" 
  VerticalPadding="0px" />
</asp:TreeView>
</div>

    Person to whom you contact:
<asp:
Label
    id="lblMessage"
    EnableViewState="False"
    Runat="server" Font-Names="Verdana" ForeColor="#3333CC" />
</div>
</form>
</body>
</html>

Output Window

TreeView.gif

Now if you select any Parent-Node or Child-Node, whatever you select will be underline and display on the page as above. 

I hope this will help you.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor