简体   繁体   English

如何在Asp.net中使用复选框构建Treeview

[英]How to build Treeview With Checkbox in Asp.net

I want to show all my directory based on the input path. 我想根据输入路径显示所有目录。 So far I achieved the treeview. 到目前为止,我已经实现了树状视图。 How can I make the Asp.net treeview with checkbox? 如何使用复选框制作Asp.net树状视图?

Here is my code 这是我的代码

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
   // ListDirectory(tvTreeView, Server.MapPath("~/"));
    DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/"));
    TreeNode mainNode = new TreeNode();

    mainNode.Text = dir.Name;
    mainNode.Checked = true;
    mainNode.NavigateUrl = "~/" + dir.Name;
    TreeView1.Nodes.Add(mainNode);
    foreach (DirectoryInfo subDir in dir.GetDirectories())
    {
        TreeNode parentNode = new TreeNode();

        parentNode.Text = subDir.Name;
        parentNode.NavigateUrl = "~/" + dir.Name + "/" + subDir.Name;


        foreach (FileInfo file in subDir.GetFiles())
        {
            TreeNode subNode = new TreeNode();


            subNode.Text = file.Name;
            subNode.NavigateUrl = "~/" + dir.Name + "/" + subDir.Name + "/" + file.Name;

            //Add it to the parent node
            parentNode.ChildNodes.Add(subNode);
        }

        TreeView1.Nodes[0].ChildNodes.Add(parentNode);
    }
}

Without fully digesting your code, I assume the problem you are having ( and didn't state ), is that the tree only shows one level of subfolders and files in those folders. 如果没有完全摘要您的代码,我认为您遇到的问题(并且没有陈述)是该树仅显示这些文件夹中的子文件夹和文件的一级。

This is a prime use-case for recursion . 这是递归的主要用例。

If this is your problem, then you can solve this with recursion with something like the following ( note - this is adhoc - so use it as a basis, taken with a grain of salt ): 如果这是您的问题,则可以使用类似以下内容的递归来解决此问题(注意-这是自组织-因此以它为基础,以一粒盐为准):

private void AddNodeForDirectory(DirectoryInfo directory, TreeNode directoryNode)
{
    foreach (DirectoryInfo subDirectory in directory.GetDirectories())
    {
        TreeNode subDirectoryNode = new TreeNode
        {
            Text = subDirectory.Name,
            NavigateUrl = // some path... I leave this to you 
        };

        foreach (FileInfo file in subDirectory.GetFiles())
        {
            TreeNode fileNode = new TreeNode
            {
                Text = file.Name,
                NavigateUrl = // some path... I leave this to you
            };  

            subDirectoryNode.ChildNodes.Add(fileNode);
        }

        directoryNode.ChildNodes.Add(subDirectoryNode);

        // Here is the recursion
        this.AddNodeForDirectory(subDirectory, subDirectoryNode);
    }
}

The idea is that you call the method, passing in your root directoryinfo, and the root treenode, and it recursively populates the tree, by drilling down into your directory infos. 想法是您调用该方法,传入根目录信息和根treenode,然后通过向下钻取目录信息来递归地填充树。 Note also, that recursion can result in stack overflows, so you should be aware of the dangers. 还要注意,递归会导致堆栈溢出,因此您应该注意这些危险。

Set your tree view's CheckBoxes property to true. 将树视图的CheckBoxes属性设置为true。

treeView1.CheckBoxes = true; treeView1.CheckBoxes = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM