Back to Devexpress

Virtual Mode

aspnet-8575-components-site-navigation-and-layout-tree-view-concepts-binding-to-data-virtual-mode.md

latest6.4 KB
Original Source

Virtual Mode

  • Jun 17, 2021
  • 3 minutes to read

In addition to bound and unbound modes, the ASPxTreeView can operate in Virtual Mode , which greatly reduces both the server load and start-up time when working with complex or dynamically created hierarchies. In Virtual Mode , data is retrieved on the server in portions, on a client request, at start-up, and when expanding nodes. In essence, child nodes are not created and initialized until their parent node is expanded for the first time. This allows you to efficiently use server resources and to avoid retrieving all the hierarchical data for the ASPxTreeView.

To activate Virtual Mode for the ASPxTreeView, handle its ASPxTreeView.VirtualModeCreateChildren event, which occurs when expanding nodes for the first time. In the event handler, you need to create a list of TreeViewVirtualNode objects representing child nodes for the currently expanded node. If a child node has no children, set its TreeViewVirtualNode.IsLeaf property to true, to prevent the display of the expand button for this node.

Note

The ASPxTreeView.SyncSelectionMode property affects the ASPxTreeView control behavior in virtual mode.

Examples

In this demo, the ASPxTreeView uses Virtual Mode to display the file/folder tree of the demo’s web site.

aspx
<dx:ASPxTreeView ID="treeView" runat="server" EnableCallBacks="true" 
    OnVirtualModeCreateChildren="treeView_VirtualModeCreateChildren" />
csharp
using System.Collections.Generic;
using DevExpress.Web.ASPxTreeView;
using System.IO;

public partial class _Default : System.Web.UI.Page {
    const string FileImageUrl = "~/Images/file.png";
    const string DirImageUrl = "~/Images/directory.png";

    protected void Page_Load(object sender, EventArgs e) {
    }

    protected void treeView_VirtualModeCreateChildren(object source, TreeViewVirtualModeCreateChildrenEventArgs e) {
        string parentNodePath = string.IsNullOrEmpty(e.NodeName) ? Page.MapPath("~/") : e.NodeName;
        List<TreeViewVirtualNode> children = new List<TreeViewVirtualNode>();
        if (Directory.Exists(parentNodePath)) {
            foreach (string childPath in Directory.GetDirectories(parentNodePath)) {
                string childDirName = Path.GetFileName(childPath);
                if (IsSystemName(childDirName))
                    continue;
                TreeViewVirtualNode childNode = new TreeViewVirtualNode(childPath, childDirName);
                childNode.Image.Url = DirImageUrl;
                children.Add(childNode);
            }
            foreach (string childPath in Directory.GetFiles(parentNodePath)) {
                string childFileName = Path.GetFileName(childPath);
                if (IsSystemName(childFileName))
                    continue;
                TreeViewVirtualNode childNode = new TreeViewVirtualNode(childPath, childFileName);
                childNode.IsLeaf = true;
                childNode.Image.Url = FileImageUrl;
                children.Add(childNode);
            }
        }
        e.Children = children;
    }

    protected bool IsSystemName(string name) {
        name = name.ToLower();
        return name.StartsWith("app_") || name == "bin"
            || name.EndsWith(".aspx.cs") || name.EndsWith(".aspx.vb");
    }
}
vb
Imports System.Collections.Generic
Imports DevExpress.Web.ASPxTreeView
Imports System.IO

Partial Public Class _Default
    Inherits System.Web.UI.Page
    Private Const FileImageUrl As String = "~/Images/file.png"
    Private Const DirImageUrl As String = "~/Images/directory.png"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    End Sub

    Protected Sub treeView_VirtualModeCreateChildren(ByVal source As Object, ByVal e As TreeViewVirtualModeCreateChildrenEventArgs)
        Dim parentNodePath As String
        If String.IsNullOrEmpty(e.NodeName) Then
            parentNodePath = Page.MapPath("~/")
        Else
            parentNodePath = e.NodeName
        End If
        Dim children As New List(Of TreeViewVirtualNode)()
        If Directory.Exists(parentNodePath) Then
            For Each childPath As String In Directory.GetDirectories(parentNodePath)
                Dim childDirName As String = Path.GetFileName(childPath)
                If IsSystemName(childDirName) Then
                    Continue For
                End If
                Dim childNode As New TreeViewVirtualNode(childPath, childDirName)
                childNode.Image.Url = DirImageUrl
                children.Add(childNode)
            Next childPath
            For Each childPath As String In Directory.GetFiles(parentNodePath)
                Dim childFileName As String = Path.GetFileName(childPath)
                If IsSystemName(childFileName) Then
                    Continue For
                End If
                Dim childNode As New TreeViewVirtualNode(childPath, childFileName)
                childNode.IsLeaf = True
                childNode.Image.Url = FileImageUrl
                children.Add(childNode)
            Next childPath
        End If
        e.Children = children
    End Sub

    Protected Function IsSystemName(ByVal name As String) As Boolean
        name = name.ToLower()
        Return name.StartsWith("app_") OrElse name = "bin" OrElse name.EndsWith(".aspx.cs") OrElse name.EndsWith(".aspx.vb")
    End Function
End Class

See Also

Online Demo: Tree View - Virtual Mode