0

我有一个用于显示目录结构的树视图。我试图通过在节点扩展上加载子节点来减少加载时间。有没有办法做到这一点?

下面是我目前用来填充树视图的代码:

protected void Page_Load(object sender, EventArgs e) {
    BuildTree(Request.QueryString["path"]);
}
private void BuildTree(string dirPath)
{
    //get root directory
    System.IO.DirectoryInfo rootDir = new System.IO.DirectoryInfo(dirPath);

    //create and add the root node to the tree view
    TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
    TreeView1.Nodes.Add(rootNode);

    //begin recursively traversing the directory structure
    TraverseTree(rootDir, rootNode);
}
private void TraverseTree(System.IO.DirectoryInfo currentDir, TreeNode currentNode)
{
    //loop through each sub-directory in the current one
    foreach (System.IO.DirectoryInfo dir in currentDir.GetDirectories())
    {
            //create node and add to the tree view
            TreeNode node = new TreeNode(dir.Name, dir.FullName);
            currentNode.ChildNodes.Add(node);

            //recursively call same method to go down the next level of the tree
            TraverseTree(dir, node);
    }

    foreach (System.IO.FileInfo file in currentDir.GetFiles())
    {
        TreeNode node = new TreeNode(file.Name, file.FullName);
        currentNode.ChildNodes.Add(node);
    }
}
4

1 回答 1

0

用于按需加载节点,这意味着只有在展开父节点时才会加载节点的子节点。执行以下步骤:

1 - 将TreeView.ExpandDepth设置为0。这消除了在 中添加的TreeNode对象的扩展,并在属性设置为true的每个对象旁边TreeView显示扩展符号[+]TreeNodeTreeNode.PopulateOnDemand

2- 将每个分支的TreeNode.PopulateOnDemand设置为True。当集合为时,扩展符号[+]将仅显示在属性设置为true的对象旁边。 TreeNodeTreeNode.ChildNodesTreeNodeTreeNode.PopulateOnDemand

3- 处理TreeView.TreeNodePopulate事件以在展开时填充分支节点。当 a TreeNode-TreeNode.PopulateOnDemand设置为true - 在事件被触发之前被展开时,这个TreeView.TreeNodeExpanded事件将被触发。

来源:ASP.NET TreeView 和按需加载数据

于 2013-02-21T00:30:59.280 回答