开发者

asp.net TreeView Expand question

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.Se开发者_JS百科lected = 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


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜