4

Im using Treeview in asp.net if i select any parent node then it should be expand and other parents shoulb be collapsed after page postback(navigation)...im using code below.

 protected void Page_Load(object sender, EventArgs e)
{
 if (!IsPostBack)
        {
            if (Session["TreeViewState"] != null)
            {
                List<string> list = (List<string>)Session["TreeViewState"];
                RestoreTreeViewState(TreeViewApplicationSetup.Nodes, list);
            }
        }
        else
        {
            List<string> list = new List<string>(100);
            SaveTreeViewState(TreeViewApplicationSetup.Nodes, list);
            Session["TreeViewState"] = list;
        }
}



 private void SaveTreeViewState(TreeNodeCollection nodes, List<string> list)
    {
        Session["TreeViewState"] = null;
        foreach (TreeNode node in nodes)
        {
            if (node.ChildNodes != null)
            {
                if (node.Expanded.HasValue && node.Expanded == true && !String.IsNullOrEmpty(node.Text))
                {
                    list.Add(node.Text);
                }
                if (node.ShowCheckBox == true && node.ChildNodes.Count == 0 && node.Parent.Expanded == true)
                {
                    if (node.Checked == true)
                    {
                        list.Add(node.ValuePath + "-T");
                    }
                    else
                    {
                        list.Add(node.ValuePath + "-F");
                    }
                }
                SaveTreeViewState(node.ChildNodes, list);
            }
        }
    }
    private void RestoreTreeViewState(TreeNodeCollection nodes, List<string> list)
    {
        foreach (TreeNode node in nodes)
        {

            if (list.Contains(node.Text) || list.Contains(node.ValuePath + "-T") || list.Contains(node.ValuePath + "-F"))
            {
                if (node.ChildNodes != null && node.ChildNodes.Count != 0 && node.Expanded.HasValue && node.Expanded == false)
                {
                    if (node.Parent != null)
                    {
                        if (list.Contains(node.ChildNodes[0].ValuePath + "T") || list.Contains(node.ChildNodes[0].ValuePath + "-F"))
                        {
                            node.Expand();
                        }
                    }
                    else
                    {
                        node.Expand();
                    }
                }
                else if (node.ChildNodes != null && node.Expanded.HasValue && node.Expanded == false)
                {
                    if (node.ShowCheckBox == true && list.Contains(node.Parent.Text) && list.Contains(node.Parent.Parent.Text))
                    {
                        if (list.IndexOf(node.ValuePath + "-T") != -1)
                        {
                            node.Checked = true;

                        }
                        else if (list.IndexOf(node.ValuePath + "-F") != -1)
                        {
                            node.Checked = false;

                        }
                    }
                }
            }
            else
            {
                if (node.ChildNodes != null && node.ChildNodes.Count != 0 && node.Expanded.HasValue && node.Expanded == true)
                {
                    node.Collapse();
                }
            }
            if (node.ChildNodes != null && node.ChildNodes.Count != 0)
            {
                RestoreTreeViewState(node.ChildNodes, list);
            }
        }
    }

its jsut help me to expand parent nodes in every postback but othere parents node didnt collapsed....

1
  • can anybody know solution for this? Commented Feb 19, 2010 at 5:50

1 Answer 1

3

In your .aspx file:

...
<asp:TreeView ID="Tree" runat="server" OnSelectedNodeChanged="Tree_SelectNodeChange">
...

Then in codebehind Page_Load you just binding data to the TreeView and in OnSelectedNodeChanged handler you need to collapse all nodes and then expand selected node and all it's parents:

protected void Tree_SelectNodeChange(object sender, EventArgs e)
{
    var tree = (TreeView)sender;
    foreach (TreeNode node in tree.Nodes)
    {
        node.CollapseAll();
    }
    ExpandToRoot(tree.SelectedNode);
}

private void ExpandToRoot(TreeNode node)
{
    node.Expand();
    if (node.Parent != null)
    {
        ExpandToRoot(node.Parent);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was trying to do something similar, mixed with the Expand event, and it was a nightmare - recursion for anything below the root node. I looked at this three times before it clicked that I should handle just the select event. Will probably add some jQuery to keep the other root nodes collapsed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.