ASP.NET Tree Control - Cannot re-expand tree on reload

I have the following code to delete a company displayed in my ASP.NET 2.0 Tree control:

protected void Delete_Click(object sender, EventArgs e)
{
    TreeNode parentNode = null;
    int expandDepth = 1;
    string companyID = "";

    expandDepth = companyTree.SelectedNode.Depth;
    if(companyTree.SelectedNode.Parent != null)
        parentNode = companyTree.SelectedNode.Parent;
    companyID = companyTree.SelectedNode.Value;

    // Delete the company...
    //// Companies.DeleteCompany(new Guid(companyID));

    // Repopulate the tree...
    DataTable dtTree = Companies.GetTree();
    companyTree.Nodes.Clear();
    companyTree.Nodes.Add(Tree.BuildTree(dtTree, Page));
    companyTree.ExpandDepth = expandDepth;
    companyTree.ShowLines = true;
    if (parentNode != null)
    {
        List<TreeNode> parentChain = new List<TreeNode>(expandDepth + 1);
        parentChain.Add(parentNode);
        while (parentNode.Parent != null)
        {
            parentChain.Add(parentNode.Parent);
            parentNode = parentNode.Parent;
        }

        for (int i = parentChain.Count - 1; i >= 0; i--)
        {
            parentChain[i].Expand();
        }
        parentChain[0].Select();
    }
}

For some reason, the tree displays as completely collapsed (root node only showing) and nothing I do seems to make it expand back to at least the parent of the node I deleted. Any suggestions?

12.10.2009 21:47:58
4 ОТВЕТА
РЕШЕНИЕ

Try this: DataBind() makes the expand depth propery kick in.

        companyTree.ExpandDepth = expandDepth;
        companyTree.ShowLines = true;
        companyTree.DataBind();
0
20.10.2009 06:18:37
Without DataBind(), the example I wrote up had all collapsed nodes too, regardless of what the ExpandDepth property was set to. I did not use any of your code after 'companyTree.ShowLines = true;', it didn't make any difference.
CRice 20.10.2009 06:54:46
Don't know why but this worked. I am not using a dataset to bind with. Very intriguing!
Keith Barrows 22.10.2009 18:58:55

If I understand correctly, companyTree is your root tree, and parentNode is a node on that tree. You seem to be clearing all nodes in companyTree, and rebuilding it, so now parentNode does not point to any node in the new tree (it's a reference to a dangling branch, if you will :)). You're expanding stuff using parentNode, but it is not relevant, since it has nothing to do with your tree control once you've cleared it.

What you should probably do is remember the ID of the company you want to select and expand all the way to, instead of saving a reference to a specific node.

0
16.10.2009 13:19:58

Get the expandDepth of the node getting deleted.

Set companyTree.ExpandDepth=the expandDepth of the deleted Node.

I think it will solve your problem.

0
20.10.2009 12:16:03
This is already happening: companyTree.ExpandDepth = expandDepth;
CRice 21.10.2009 00:16:30

Try the following:

protected void Delete_Click(object sender, EventArgs e)
{
    TreeNode parentNode = null;
    int expandDepth = 1;
    string companyID = "";

    expandDepth = companyTree.SelectedNode.Depth;
    if(companyTree.SelectedNode.Parent != null)
        parentNode = companyTree.SelectedNode.Parent;
    companyID = companyTree.SelectedNode.Value;

    // Delete the company...
    //// Companies.DeleteCompany(new Guid(companyID));

    // Repopulate the tree...
    DataTable dtTree = Companies.GetTree();
    companyTree.Nodes.Clear();
    companyTree.Nodes.Add(Tree.BuildTree(dtTree, Page));
    companyTree.ExpandDepth = expandDepth;
    companyTree.ShowLines = true;
    while(parentNode.Parent!= null)
    {
     parentNode.Expand();
     parentNode= parentNode.Parent;
    }
    parentNode.Expand();
}
0
21.10.2009 12:06:11