Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » ASP.NET Controls » TreeView in ASP.NET

TreeView in ASP.NET

In this article, I am going to discuss how to create a TreeView control in ASP.NET 3.5.

Author Rank :
Page Views : 13373
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Introduction:

The TreeView control is one of the most important controls of ASP.NET controls. It is used to display records in a hierarchical format.

TreeView.png 

Figure 1: TreeView control structure

How to use TreeView control: Follow these steps:

Step 1: Open Visual Studio 2008 and drag a TreeView control from the toolbar and drop on page as follows:

image1.png
 

Figure 2:

Step 2: Copy this code and paste on your page.

<asp:TreeView ID="CourseTreeView" runat="server" OnSelectedNodeChanged="CourseTreeView_SelectedNodeChanged" ShowLines="true" DataSourceID="XmlDataSource1" ExpandDepth="1" PopulateNodesFromClient="false">
    <Nodes>
        <asp:TreeNode Text="Puru" PopulateOnDemand="false" Value="-1" />
    </Nodes>
</asp:TreeView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/treenode.xml">
</asp:XmlDataSource>

Here I am binding the node from a xml file. This is optional. You can bind a TreeView node from a database or fix the node directly within the <node> tab.

How to open a popup upon a click of any node in a TreeView?

I am using a small JavaScript function for popup on click.

<script type="text/javascript" language="javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        function EndRequestHandler(sender, args) {
            alert($get("<%=TextBox1.ClientID %>").value);
        }
</script>

The following example demonstrates how to use a TreeView control and how to popup on click of any node.

Example:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body
>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:TreeView ID="CourseTreeView" runat="server" OnSelectedNodeChanged="CourseTreeView_SelectedNodeChanged"
                ShowLines="true" DataSourceID="XmlDataSource1" ExpandDepth="1" PopulateNodesFromClient="false">
                <Nodes>
                    <asp:TreeNode Text="Puru" PopulateOnDemand="false" Value="-1" />
                </Nodes>
            </asp:TreeView>
            <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/treenode.xml">
            </asp:XmlDataSource>
            <span style="display: none;">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </span>
        </ContentTemplate>
    </asp:UpdatePanel>

    
<script type="text/javascript" language="javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        function EndRequestHandler(sender, args) {
            alert($get("<%=TextBox1.ClientID %>").value);
        }
    </script>

    
</form>
</body>
</html>
 

protected void CourseTreeView_SelectedNodeChanged(object sender, EventArgs e)
{
    TextBox1.Text = CourseTreeView.SelectedNode.Text;
}

Output:

output.png
 

Figure 3: Output of TreeView control.

If you click on any tree node, a popup will be shown as follows:

popup.png
 

Figure 4: PopUp shown upon click of a tree node.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by rajesh kasani on Jul 14, 2011
hi,

  thanks for the example sharing.

  but does this menu navigate.

  can i have details of the treenode.xml that u used for the example.
 
  thanks

bye
 [Top] Rate this article
 
 About the author
 
Purushottam Rathore

I am working as a Software Developer and has 4 years of experience on Microsoft Technology and having a Master Degree in Computer Application. I really like to work in the .NET platform. and working with ASP.NET 2.0/3.5, Web Services, WPF, WCF, Silverlight, AJAX, JavaScript, JQuery, Ado.net, MsAccess, SQL Server 2005/2008.

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
tree view code behind page by Scott On February 2, 2011
I was wondering if you could post or email the code behind page for getting more than 2 levels to work on the tree view. Javascript works great. Thanks.
Reply | Email | Modify 
iiiii by mahmut sami On March 31, 2011
http://3d2f.com/programs/17-386-9rays-net-treeview-for-asp-net-download.shtml
Reply | Email | Modify 
upload project by Suthish On May 31, 2011
Purushottam, Also upload sample project. Help full for members.
Reply | Email | Modify 
jh, by gaurav On July 7, 2011
jh,,
Reply | Email | Modify 
question by vinoth On July 8, 2011
in tree view..we r creating the design..then wat we want to do...
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.