views:

7

answers:

1

Hi guys, hope you help me with I think a simple TreeView Expand problem.

I have a TreeView control in my MasterPage and my default depth is 2 and I see that when I click on the deeper node it keeps expanded.. But when I redirected into another page, the node collapsed.

I have a problem with my code which suppose to keep the node expanded.

TreeNode thisNode = tvCategories.FindNode(Session["SelectedCIDValPath"].ToString());
if (thisNode != null)
{
    thisNode.Selected = true;
    thisNode.Expand();
    thisNode.Select();
    thisNode.Expanded = true;
    lbl.Text = "valupath: " + Session["SelectedCIDValPath"].ToString();
}

as you can see, I tried all the possible properties and methods to keep the deeper node expanded.. but it doesn't work.

Please help me? Thank you so much

A: 

It happens to be the case (and I find it just a bit frustrating) that expanding a node does not also cause parent nodes to expand. In order to ensure a node expands, it is necessary to also ensure that the parent nodes expand. I keep an extension method handy for this purpose:

public static void EnsureExpanded(this TreeNode node)
{
    if (node != null)
    {
        EnsureExpanded(node.Parent);
        node.Expand();
    }
}

You can employ the extension like so:

TreeNode thisNode = tvCategories.FindNode(Session["SelectedCIDValPath"].ToString());
thisNode.EnsureExpanded();
kbrimington