views:

329

answers:

4

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?

A: 

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.

Doron Yaacoby
A: 

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

        companyTree.ExpandDepth = expandDepth;
        companyTree.ShowLines = true;
        companyTree.DataBind();
CRice
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
Don't know why but this worked. I am not using a dataset to bind with. Very intriguing!
Keith Barrows
A: 

Get the expandDepth of the node getting deleted.

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

I think it will solve your problem.

Himadri
This is already happening: companyTree.ExpandDepth = expandDepth;
CRice
A: 

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();
}
Himadri