Closing Treeview nodes automatically

Discussion in 'ASP .Net' started by David Jackson, Oct 28, 2007.

  1. Hello,

    I have a TreeView control with "top-level" nodes and "second-level" nodes
    (probably not the correct terms) as follows:

    objNode = new TreeNode();
    objNode.Text = "2006";
    objNode.SelectAction = TreeNodeSelectAction.Expand;
    tvSide.Nodes.Add(objNode);

    objChildNode = new TreeNode();
    objChildNode.Text = " ·January";
    objChildNode.NavigateUrl = "../2006/jan.aspx";
    objNode.ChildNodes.Add(objChildNode);

    objChildNode = new TreeNode();
    objChildNode.Text = " ·February";
    objChildNode.NavigateUrl = "../2006/feb.aspx";
    objNode.ChildNodes.Add(objChildNode);

    // other child nodes for 2006

    objNode = new TreeNode();
    objNode.Text = "2007";
    objNode.SelectAction = TreeNodeSelectAction.Expand;
    tvSide.Nodes.Add(objNode);

    objChildNode = new TreeNode();
    objChildNode.Text = " ·January";
    objChildNode.NavigateUrl = "../2007/jan.aspx";
    objNode.ChildNodes.Add(objChildNode);

    objChildNode = new TreeNode();
    objChildNode.Text = " ·February";
    objChildNode.NavigateUrl = "../2007/feb.aspx";
    objNode.ChildNodes.Add(objChildNode);

    When I click on the 2006 node, it correctly expands to reveal the nodes
    underneath it.

    But, if I then click on the 2007 node, I would like it to "close up" the
    2006 node as well as expanding the 2007 node. Is there a way to do this?

    Thank you.

    DJ
     
    David Jackson, Oct 28, 2007
    #1
    1. Advertising

  2. Hi, I have done this in Javascript. You can actually view source of the page
    and write a generic code to handle it.

    -Geo.

    "David Jackson" wrote:

    > Hello,
    >
    > I have a TreeView control with "top-level" nodes and "second-level" nodes
    > (probably not the correct terms) as follows:
    >
    > objNode = new TreeNode();
    > objNode.Text = "2006";
    > objNode.SelectAction = TreeNodeSelectAction.Expand;
    > tvSide.Nodes.Add(objNode);
    >
    > objChildNode = new TreeNode();
    > objChildNode.Text = " ·January";
    > objChildNode.NavigateUrl = "../2006/jan.aspx";
    > objNode.ChildNodes.Add(objChildNode);
    >
    > objChildNode = new TreeNode();
    > objChildNode.Text = " ·February";
    > objChildNode.NavigateUrl = "../2006/feb.aspx";
    > objNode.ChildNodes.Add(objChildNode);
    >
    > // other child nodes for 2006
    >
    > objNode = new TreeNode();
    > objNode.Text = "2007";
    > objNode.SelectAction = TreeNodeSelectAction.Expand;
    > tvSide.Nodes.Add(objNode);
    >
    > objChildNode = new TreeNode();
    > objChildNode.Text = " ·January";
    > objChildNode.NavigateUrl = "../2007/jan.aspx";
    > objNode.ChildNodes.Add(objChildNode);
    >
    > objChildNode = new TreeNode();
    > objChildNode.Text = " ·February";
    > objChildNode.NavigateUrl = "../2007/feb.aspx";
    > objNode.ChildNodes.Add(objChildNode);
    >
    > When I click on the 2006 node, it correctly expands to reveal the nodes
    > underneath it.
    >
    > But, if I then click on the 2007 node, I would like it to "close up" the
    > 2006 node as well as expanding the 2007 node. Is there a way to do this?
    >
    > Thank you.
    >
    > DJ
    >
    >
    >
     
    =?Utf-8?B?R2Vv?=, Oct 28, 2007
    #2
    1. Advertising

  3. "Geo" <> wrote in message
    news:...

    Hello Geo,

    > Hi, I have done this in Javascript. You can actually view source of the
    > page
    > and write a generic code to handle it.


    Excellent suggestion! The following code will do this, if anyone else is
    interested:

    <script type="text/javascript">
    var base_TreeView_ToggleNode = TreeView_ToggleNode;
    TreeView_ToggleNode = function(data, index, node, lineType, children)
    {
    var objTreeView = document.getElementById('<%=tvSide.ClientID%>');
    for (intChild = 0; intChild < objTreeView.children.length;
    intChild++)
    {
    if (objTreeView.children[intChild].id.indexOf('Nodes') > -1)
    {
    objTreeView.children[intChild].style.display = "none";
    }
    }
    base_TreeView_ToggleNode(data, index, node, lineType, children);
    }
    </script>

    N.B. this could probably be made more robust by checking that the id of the
    children element ends with 'Nodes' rather than just contains 'Nodes', and
    the TreeView_ToggleNode function does a fair bit more than just setting the
    display style, but the above works for me and is all I need at the moment.

    DJ
     
    David Jackson, Oct 29, 2007
    #3
  4. Closing Nodes upon Selection of anotherone

    Hi guys. I have a treeview that should close is nodes when one of them is expanded just as this example. But there is a "catch"-

    My treeview structure is as follows

    RootNode
    --------Child1
    ----------Sub1Child1
    ----------Sub1Child2
    --------Child2
    ----------Sub2Child1
    ----------Sub2Child2

    If I expand Sub1Child1 then Sub1Child2 and Child2 should be Collapsed.

    Can you give me a hand here? I've been trying to do this for some time now and got no results :S

    Thanks

    EggHeadCafe - .NET Developer Portal of Choice
    http://www.eggheadcafe.com
     
    Matias Banzas, Dec 5, 2007
    #4
  5. David Jackson

    Geo Guest

    RE: Closing Nodes upon Selection of anotherone

    Hi, Just wondering whether you got to do this. If not let me know and I can
    help you. I did the same in one of my projects.

    Thanks
    Geo.

    "Matias Banzas" wrote:

    > Hi guys. I have a treeview that should close is nodes when one of them is expanded just as this example. But there is a "catch"-
    >
    > My treeview structure is as follows
    >
    > RootNode
    > --------Child1
    > ----------Sub1Child1
    > ----------Sub1Child2
    > --------Child2
    > ----------Sub2Child1
    > ----------Sub2Child2
    >
    > If I expand Sub1Child1 then Sub1Child2 and Child2 should be Collapsed.
    >
    > Can you give me a hand here? I've been trying to do this for some time now and got no results :S
    >
    > Thanks
    >
    > EggHeadCafe - .NET Developer Portal of Choice
    > http://www.eggheadcafe.com
    >
     
    Geo, Jan 10, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lanny McDonald
    Replies:
    1
    Views:
    351
    Alvin Bruney [MVP]
    Apr 3, 2004
  2. asd
    Replies:
    3
    Views:
    458
    Arnaud Berger
    May 23, 2005
  3. gavnosis
    Replies:
    0
    Views:
    544
    gavnosis
    Aug 2, 2003
  4. Per Salmi

    TreeView not rendering nodes? Cacheing of a TreeView

    Per Salmi, Oct 15, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    514
    Steve Drake
    Oct 15, 2003
  5. Lanny McDonald

    Help - Recursively Opening & Closing All Nodes in Treeview

    Lanny McDonald, Apr 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    118
    Lanny McDonald
    Apr 2, 2004
Loading...

Share This Page