展开和折叠树视图

问题描述:



我正在使用treeview,在其中我只想扩展选中的node ,而其他的则是折叠.我使用了代码来执行此操作,但是我想知道如何在pageload 运行代码的地方.


我的代码如下:



I am using a treeview,in which i want to expand that node only which is selected,and others are collapse.I have used a code for doing this,but i want to know that how to bind the tree on pageload by which the code is run.


my code is given below:

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




感谢




thanks

我假设这是WinForm. WinForm上的所有内容都是手动的,这意味着您必须折叠每个节点.我建议您保留一个指向最近扩展的节点的指针,并在选定的节点事件上保留指向该节点的指针,并折叠该节点及其父节点.
I am assuming that this is WinForm. Everything on WinForm is manual, which means that you will have to collapse every node. I would recommend that you keep a pointer to the most recently expanded node, and on the node selected event, to to that node and collapse the node and its parents.