Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 5th, 2006, 10:16 AM
Friend of Wrox
 
Join Date: Feb 2006
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
Default trying to achieve a specific Treeview Behavior

I am using a treeview for navigation purposes and I want a very specific behavior that I can not seem to find any examples of.

What I want is for the first level to show only on the top page. Then if you click a menu item that has child nodes, that node would expand only. This effect would be recursive or continuous down the tree. Only one node on each level could be expanded at any given time. Perhaps this is best described in examples:

Initial menu:

Top Level Menu Item 1
Top Level Menu Item 2
Top Level Menu Item 3
Top Level Menu Item 4

Then after you click Top Level Menu Item 2 you would see:


Top Level Menu Item 1
Top Level Menu Item 2
     Level 2, Menu Item 2.1
     Level 2, Menu Item 2.2
     Level 2, Menu Item 2.3
Top Level Menu Item 3
Top Level Menu Item 4


At this point, if you clicked Top Level Menu Item 3, then it would collapse the children of Top Level Menu Item 2, and display the children nodes of Top Level Menu Item 3, thus you would see:

Top Level Menu Item 1
Top Level Menu Item 2
Top Level Menu Item 3
     Level 2, Menu Item 3.1
     Level 2, Menu Item 3.2
     Level 2, Menu Item 3.3
Top Level Menu Item 4


Then, if you clicked one of the children, it would expand it:

Top Level Menu Item 1
Top Level Menu Item 2
Top Level Menu Item 3
     Level 2, Menu Item 3.1
     Level 2, Menu Item 3.2
          Level 3, Menu Item 3.2.1
          Level 3, Menu Item 3.2.2
          Level 3, Menu Item 3.2.3
     Level 2, Menu Item 3.3
Top Level Menu Item 4


So only one level 2 node, and only one level 3 node can be expanded at a time. Clicking a different level 2 node would collapse the currently expanded node and expand the clicked node. I don't want this to be client side. It will post back anyway, because each child node you click links to a page that will be loaded so there is no purpose to this behavior being implemented on the client side. Here is my current code, which is very basic:

Code:
    <asp:TreeView runat="server" ID="trVwAssociations" 
        CssClass="subMenuArea" 
        DataSourceID="SiteMapDataSource1" 
        ShowExpandCollapse="false" 
        NodeIndent="15" 
        LeafNodeStyle-VerticalPadding="4" 
        ParentNodeStyle-VerticalPadding="4"
        SelectedNodeStyle-VerticalPadding="4" 
        RootNodeStyle-VerticalPadding="4" 
        MaxDataBindDepth="3" 
        ExpandDepth="0" 
        PopulateNodesFromClient="false" 
        EnableClientScript="false" 
        EnableViewState="false" />
    <asp:SiteMapDataSource runat="server" ID="SiteMapDataSource1"  
        StartFromCurrentNode="false" 
        ShowStartingNode="false"  
        StartingNodeUrl="/Associations/Default.aspx" />
Neil Timmerman
Programmer
Veris Consulting
 
Old September 8th, 2006, 12:25 AM
Friend of Wrox
 
Join Date: Feb 2006
Posts: 116
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, after dicking with it for a couple of hours (that I really didn't have) I did figure this out. In the Master Page, you can handle the TreeViews onDataBind event, iterate through the first level nodes, and expand as needed from there.

Code:
public partial class MasterPages_associations : System.Web.UI.MasterPage
{

    protected void trVwAssociations_DataBind(object sender, EventArgs e)
    {

        /* I can't do the first level in the recursive function because 
         * I had to not include the root node in order for the root node 
         * not to show...so I have to loop through level 1 nodes, and THEN 
         * begin recursion. */

        for (int i = 0; i < trVwAssociations.Nodes.Count; i++)
        {
            expandSelectedNodes(trVwAssociations.Nodes[i], Request.RawUrl);
        }
    }

    /* Recursively iterates through the tree in PreOrder and expands nodes as needed */
    private void expandSelectedNodes(TreeNode node, string url)
    {
        if (node.NavigateUrl == url) node.Expand();

        if (node.ChildNodes.Count > 0)
            for (int i = 0; i < node.ChildNodes.Count; i++)
                expandSelectedNodes(node.ChildNodes[i], url);

        return;
    }

}
By the way if anyone knows a more direct approach, I would LOVE to have it. I could not for the life of me figure out how to obtain the TreeNode's index, so I can not use the available method to expand exactly the node I want.

There HAS to be a way to get it. Surely Microsoft doesn't indend for tree's to be iterated through in order to expand them like this. It doesn't seem very efficient, although I will admit, for navigation, these tree's really can't ever be very large or they wouldn't make for very good navigation controls...so perhaps the performance is not really an issue here....but still...

Neil Timmerman
Programmer
Veris Consulting





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to achieve dynamic binding li72 XSLT 3 February 21st, 2008 11:46 AM
Search specific data from specific columns yogeshyl SQL Language 1 January 16th, 2008 11:12 AM
Copy specific data in specific cells of sheet2 yogeshyl Excel VBA 1 May 14th, 2007 07:40 AM
how to achieve static method in an interface? cfouquet C# 3 September 28th, 2004 08:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.