Skip to main content

Virtual Mode

  • 3 minutes to read

In addition to bound and unbound modes, the ASPxTreeView supports Virtual Mode. This mode significantly reduces server load and startup time for complex or dynamically generated hierarchies. In Virtual Mode, the control retrieves data from the server in portions: at startup and when users expand nodes. Child nodes are created and initialized only when their parent node is expanded for the first time. This approach improves server resource usage and removes the need to load the entire hierarchy upfront.

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.

<dx:ASPxTreeView ID="treeView" runat="server" EnableCallBacks="true" 
    OnVirtualModeCreateChildren="treeView_VirtualModeCreateChildren" />
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");
    }
}
See Also