开发者

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.开发者_如何学JAVAParent;
    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?


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

        companyTree.ExpandDepth = expandDepth;
        companyTree.ShowLines = true;
        companyTree.DataBind();


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.


Get the expandDepth of the node getting deleted.

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

I think it will solve your problem.


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜