简体   繁体   中英

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?

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. 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.

treeView1.CheckBoxes = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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