在Treeview Asp.net中隐藏rootnode [英] Hide rootnode in treeview asp.net

查看:393
本文介绍了在Treeview Asp.net中隐藏rootnode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,如何隐藏根节点"temp"文件夹?我想在rootnode上设置CssClass来完成此任务.

How can I hide the rootnode in this case the "temp" folder? I want to do this whitout setting a CssClass on the rootnode.

TreeView TreeView1 = new TreeView();
    protected void Page_Load(object sender, EventArgs e)
    {
        BuildTree(@"C:\temp");
        form1.Controls.Add(TreeView1);
    }
    private void BuildTree(string root)
    {
        DirectoryInfo rootDir = new DirectoryInfo(root);
        TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
        TreeView1.Nodes.Add(rootNode);
        TraverseTree(rootDir, rootNode);
    }
    private void TraverseTree(DirectoryInfo currentDir, TreeNode currentNode)
    {
        foreach (DirectoryInfo dir in currentDir.GetDirectories())
        {
            TreeNode node = new TreeNode(dir.Name, dir.FullName);
            currentNode.ChildNodes.Add(node);
            TraverseTree(dir, node);
        }
        foreach (FileInfo file in currentDir.GetFiles())
        {
            TreeNode nodeFile = new TreeNode(file.Name, file.FullName);
            currentNode.ChildNodes.Add(nodeFile);
        }
    }

该代码完整且可重复运行,只需更改桌面的路径即可.

The code is complete and redy to run just change the path to your desktop.

推荐答案

为什么不首先不添加Root节点,而是将所有直接后代设置为1级节点:

Why not just not add the Root node in the first place, instead, set all the direct descendants as level 1 nodes:

TreeView TreeView1 = new TreeView();
protected void Page_Load(object sender, EventArgs e)
{
    BuildTree(@"C:\temp");
    form1.Controls.Add(TreeView1);
}
private void BuildTree(string root)
{
    DirectoryInfo rootDir = new DirectoryInfo(root);
    TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
    TraverseTree(rootDir, TreeView1.Nodes);
}
private void TraverseTree(DirectoryInfo currentDir, TreeNodeCollection nodeCollection)
{
    foreach (DirectoryInfo dir in currentDir.GetDirectories())
    {
        TreeNode node = new TreeNode(dir.Name, dir.FullName);
        nodeCollection.Add(node);                
        TraverseTree(dir, node.ChildNodes);
    }
    foreach (FileInfo file in currentDir.GetFiles())
    {
        TreeNode nodeFile = new TreeNode(file.Name, file.FullName);
        nodeCollection.Add(nodeFile);
    }
}

我已经修改了上面的代码,以删除我编写的 AddToNode 方法.先前的方法是检查传入的currentNode对象是否为null,如果是,则将其添加到TreeView1的NodesCollection中(否则添加到currentNode的ChildNodes集合中).相反,我没有传递节点,而是传递了节点的NodeCollection-这意味着我们可以合理地简化逻辑.)

I have amended the code above to remove the AddToNode Method I wrote. The previous method was checking to see if the currentNode object passed in was null, and if so adding it to the NodesCollection of TreeView1 (otherwise to the ChildNodes collection of the currentNode). Instead, rather than passing the node around, I'm passing the NodeCollection of the node around - this means that we can simplify the logic a fair bit).

这篇关于在Treeview Asp.net中隐藏rootnode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆