ASP.NET TreeView + AJAX

本文关键字:AJAX TreeView NET ASP | 更新日期: 2023-09-27 18:00:28

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

以下是我目前用于填充树视图的代码:

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);
    }
}

ASP.NET TreeView + AJAX

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

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

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

3-处理TreeView.TreeNodePopulate事件以在扩展时放置分支节点。当TreeNodeTreeNode.PopulateOnDemand设置为true)在TreeView.TreeNodeExpanded事件被激发之前展开时,将激发此事件。

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