页面有个树形控件ASPXListTree,可以无限级的添加,也就是可以多层,还有个按钮。我想在按钮的Click事件中实现如下功能:
先选择树形结构的某行,比如我点击第一层“资产部”,点击按钮就生成资产部这个文件夹,我点击第二层“设备科”就在资产部下面生成设备科的文件夹.........无限级的。
那么在代码里应该怎样写呢?用遍历吗,还是有别的办法,现在没什么思路,请大家帮忙,谢谢!

解决方案 »

  1.   

    你都已经把解决方案说出来了用一个树嘛,资产部门(路径 zc)
      设备科(路径 sb)在点击设备科的时候,往上寻找,直至根节点,然后依次把路径加起来就行了 sc+"\"+sbp.s. 这里的sb没有任何其他意思...表误会. ..
      

  2.   

    好像有一种可以直接绑定数据库的方案,不过我没有用过。你可以看看你所用的TreeView,有没有设置数据源的项目。
      

  3.   

    private void GetDirectoryPath(TreeViewNode node, ref string path)
    {
         if (node == null)
             return;     if (path == null) 
             path = string.Empty;     path = node.Text + Path.DirectorySeparatorChar + path;
         if (node.Parent != null)
             GetDirectoryPath(node.Parent, ref path);
    }调用:
    string path = string.Empty;
    GetDirectoryPath(treeView.SelectedNode, ref path);
      

  4.   

    如果你的TreeView的数据源是从数据库中读出来的话,那么理论上你只要知道了上级节点,那么路径就能用sql查出来,不用遍历整个树。
      

  5.   

    是从数据库中读出来的,那怎么用sql查出来呢?谢谢指教
      

  6.   

    我说下我的方法:
    1.数据库的读取,还是建议一次性读取完比较好, 如果每次只读取一部分的话 后面很有可能会频繁连接数据库,这在设计上是不合理的。当然用递归读取树形的数据库效率是相当低的,你用多线程也不会很快。我曾经用的方法就是 在数据里面增加一个字段 这个字段就是路径字段 如:宇宙《银河系《太阳系《地球《中国《。 这样你搜索的是就按照这个这段来搜,相当的快 如你要找地球下面的所有的:select * from tablename where path like '宇宙《银河系《太阳系《地球%'. 这里有参考
    2.对于生成treeview问题 这里我建议不要一次生成完 这样数据量大的时候很容易造成界面假死的现象。我的思路是 最开始只显示节点下面的一层,当点击某个节点的时候在显示此节点的下一层。这样速度也很快,也不至于有死机的问题。
    参考代码:        #region TreeView AfterSelect Event.
            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                this.getCon();
                string name = this.treeView1.SelectedNode.Text.ToString();
                this.treeView1.SelectedNode.Nodes.Clear(); // Clear the selected node's children nodes.            SortedList<string, int> childern = findUnder(name); // Find the child nodes from the data.            foreach (KeyValuePair<string, int> s in childern)
                {
                    if (data.Contains(s.Key)) // Check the srearch contain the nodes.
                    {
                         TreeNode child = new TreeNode(s.Key.ToString());
                         this.treeView1.SelectedNode.Nodes.Add(child); // Add the found nodes into the selected node.
                         data.Remove(s.Key);
                    }
                }
                for (int k = 0; k < data.Count; k++) 
                {
                    TreeNode child = new TreeNode(data[k].ToString());
                    this.treeView1.Nodes.Add(child);
                }
                this.treeView1.SelectedNode.Expand();
                this.showInfo(name); // Show the selected node's information.
                this.odcConnection.Close();// Close the data connection.
            }最后附上我的联系方式, 以后有机会大家一起探讨一下
    QQ:287072382
    MSN:[email protected]