SIGN UP MEMBER LOGIN:    
ARTICLE

TreeView in ASP.NET

Posted by Purushottam Rathore Articles | ASP.NET Controls in C# August 30, 2010
In this article, I am going to discuss how to create a TreeView control in ASP.NET 3.5.
Reader Level:

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.

Login to add your contents and source code to this article
Article Extensions
Contents added by dassd dsas on May 26, 2012
dfdsfdsfdsfsfsdfds
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
share this article :
post comment
 

in tree view..we r creating the design..then wat we want to do...

Posted by vinoth kumar Jul 08, 2011

jh,,

Posted by gaurav Jul 07, 2011

Purushottam, Also upload sample project. Help full for members.

Posted by Suthish Nair May 31, 2011

http://3d2f.com/programs/17-386-9rays-net-treeview-for-asp-net-download.shtml

Posted by mahmut sami yazar Mar 31, 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.

Posted by Scott K Feb 02, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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
Team Foundation Server Hosting
Become a Sponsor