Click here to Skip to main content
Licence CPOL
First Posted 30 Oct 2009
Views 9,452
Downloads 473
Bookmarked 13 times

A Collapsible TreeView in MasterPage

By | 30 Oct 2009 | Article
Demonstrates how to tweak the TreeView control to make it collapsible when working with MasterPages.

Introduction

Scenario: I wanted a collapsible menu (treeview) that will only show menu items from the current section/department. E.g. if in the HR section node, only show the HR submenu items on the menu. I had to use this menu on a MasterPage (that certainly complicates things!).

Using a TreeView control on a MasterPage can be a menace because the control is refreshed every time you go to another page. There are several solutions to this problem. I once came across a solution where you had to load the control's structure inside the viewstate; I also got a suggestion to 'buy' (hey, I am a developer) a control.

I decided to tweak the existing TreeView control. When I load the page, I will check the page's path and get the corresponding node from the TreeView. Then, I process the nodes as I wish.

Using the code

As the nodes are being bound to the TreeView, we check to see if the current node is pointing to the current page. If so, we set that node to be the selected node.

The rest is history. Play around with the parent nodes of the selected node in any way you wish. You can also specify default behavior, when a page that is not in the Sitemap is opened.

protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
    //make the node that links to the current page the selected node.
    string currPage = Request.CurrentExecutionFilePath;

    if (currPage == e.Node.NavigateUrl)
    {
        e.Node.Select();
    }
}

protected void ProcessChildNode(string selNodePath, TreeNode tsn)
{
    //check child nodes
    if (tsn.ChildNodes.Count > 0)
    {

        foreach (TreeNode tsnChild in tsn.ChildNodes)
        {
            //use recursion for deeper tree levels
            if (tsnChild.ChildNodes.Count > 0)
            {
                //process child node
                ProcessChildNode(selNodePath, tsnChild);
            }
            if (selNodePath.Contains(tsnChild.ValuePath.ToString()))
            {
                tsnChild.Expand();
            }
            if (tsnChild.ChildNodes.Count > 0)
            {
                tsnChild.SelectAction = TreeNodeSelectAction.Expand;
            }
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rob D.D

Web Developer

South Africa South Africa

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalproblem facing showing error linq namespace does not exists in namespace Pinmemberpinky_sood11:43 2 May '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 30 Oct 2009
Article Copyright 2009 by Rob D.D
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid