Creating a Treeview Menu in ASP.NET with C#

Introduction

Most Web sites/Web applications have menus to navigate the site, but either you have to use complex DHTML or a third-party tool to provide the menu. Instead, you can use the Treeview control shipped with Internet Explorer's Web controls to solve this problem.

Background

Internet Explorer Web controls is shipped with controls that can be used to provide better site navigation to users visiting your site. This article looks at the Treeview control. Other controls, such as the Tab strip control, also ship with the IE Web Controls. I leave those for you to explore.

Using the Code

Download http://www.asp.net/IEWebControls/Download.aspx?tabindex=0&tabid=1. Install the downloaded file. Go to the IE Web Controls <install> directory and edit the Build.bat to add a path to csc.exe (by, default csc.exe is found in C:\WINNT\Microsoft.NET\Framework\<framework version>). Execute Build.bat from the command prompt. A DLL file by the name of Microsoft.Web.UI.WebControls.dll is created in the <IE Web Controls>/build directory. Follow these steps:

  1. Copy the DLL file to the bin directory of the new application's folder or add a reference to it (VS.Net users).
  2. Register the control using the Register tag <%@ Register TagPrefix="ie" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls"%>.
  3. Add the control using <ie:TreeView runat="server" ID= "Treeview1"></ie:treeview>.
  4. Inside the .cs file, use the namespace Microsoft.Web.UI.WebControls.
  5. Use the following code to programatically add the values to the Treeview control.
  6. Declare a variable as protected: Microsoft.Web.UI.WebControls.TreeView Treeview1;
  7. /// <summary>
    /// The following function adds two master levels and their
    /// respective child nodes to the Treeview control
    /// </summary>
    public void PopulateTree()
    {
       TreeNode nodeProdDetail, nodeProdMaster;
       nodeProdMaster      = new TreeNode();
       nodeProdMaster.Text = "Master1";
       nodeProdMaster.ID   = "1";
    
       nodeProdDetail             = new TreeNode();
       nodeProdDetail.Text        = "Detail1";
       nodeProdDetail.ID          = "1.1";
       nodeProdDetail.NavigateUrl = "SomePage.aspx";
       nodeProdMaster.Nodes.Add(nodeProdDetail);
    
       Treeview1.Nodes.Add(nodeProdMaster);
    
       nodeProdMaster      = new TreeNode();
       nodeProdMaster.Text = "Master2";
       nodeProdMaster.ID   = "2";
    
       nodeProdDetail             = new TreeNode();
       nodeProdDetail.Text        = "Detail2";
       nodeProdDetail.ID          = "2.1";
       nodeProdDetail.NavigateUrl = "SomeOtherPage.aspx";
       nodeProdMaster.Nodes.Add(nodeProdDetail);
    
       Treeview1.Nodes.Add(nodeProdMaster);
    }
    


About the Author

Rohit Kukreti

He has been in Software field for last 11+ years. Always been programming on Microsoft technologies. He started with VB, ASP and then gradually moved on to ASP.Net. C# is the preferred language for development. When not working on some piece of code he likes to play table tennis and football. He is a huge fan of English Football club Manchester United.

Downloads

Comments

  • how to create mlm in ASp.net

    Posted by Neeraj on 05/21/2012 04:29am

    i am create a mlm website but how to desgine mlm tree in asp.net .any body now pleases help me

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds