Virtual Mode

  • 6 min to read

In addition to bound and unbound modes, ASPxTreeList can operate in virtual mode, which greatly reduces both the server load and start-up time when working with complex or dynamically created data. In virtual mode, a tree list is created on demand, i.e., child nodes are created and initialized when their parent node is expanded. In virtual mode, the ASPxTreeList control does not remember a collection of already created nodes, because their number might be very large. So, all nodes are created only if they are shown and you have to build the tree anew each time, from the root down to the subnodes of the last expanded node, when a user expands or collapses a node.

To implement a virtual mode for ASPxTreeList, you should handle the two following events.

You can also handle the ASPxTreeList.VirtualModeNodeCreated event, which is raised for each node after is has been created to change the required characteristics of a node.

 

Note
  • In the virtual mode, ASPxTreeList doesn't automatically create columns. Data columns should be created manually.
  • Don't add any nodes manually in the virtual mode, e.g., by using the ASPxTreeList.AppendNode method.
  • You can't get a node if it is invisible or if its parent node is collapsed, because it does not exist. Therefore, there are some limitations when you work with node selection in virtual mode:

ASPxTreeList Virtual Events Sequence

When a virtual tree list is created for the first time, the events listed in the table below are raised.

Tree List Virtual Event Typical Use
The VirtualModeCreateChildren event fires for the root node. Assign a list of root node child nodes to the event parameter's Children property. The NodeObject property returns null (Nothing for VB).
The VirtualModeNodeCreating fires for each root node child. Set the event parameter's NodeKeyValue property to a unique value, but it must remain the same value for the entire life of the tree list. Also, set the node cell values here.

After that, a tree list with first-level nodes is displayed. When an end-user expands or collapses a node, the tree list is created anew. The events are raised in the following sequence.

Tree List Virtual Event Typical Use
The VirtualModeCreateChildren event fires for the root node. Assign a list of root node child nodes to the event parameter's Children property. The NodeObject property returns null (Nothing for VB).
The VirtualModeNodeCreating fires for each root node child. Set the event parameter's NodeKeyValue property to the same unique value as before. Also, set the cell values again.
The VirtualModeCreateChildren event fires again for the node being expanded. Assign a list of subnodes to the event parameter's Children property. The NodeObject property returns a node currently being processed.
The VirtualModeNodeCreating fires for each subnode. Set the subnode's keys and values.

Example

In this example, the ASPxTreeList uses the Virtual data binding method to display the file/folder tree. In this mode, a tree is created on demand.

The image below shows the result.

exVirtualMode

using System.IO;
using System.Collections.Generic;
using DevExpress.Web.ASPxTreeList;

protected void ASPxTreeList1_VirtualModeCreateChildren(object sender,
TreeListVirtualModeCreateChildrenEventArgs e) {
    string path = e.NodeObject == null ? Page.MapPath("~/") : e.NodeObject.ToString();

    List<string> children = new List<string>();
    if (Directory.Exists(path)) {
        foreach (string name in Directory.GetDirectories(path)) {
            if (!IsSystemName(name))
                children.Add(name);
        }
        foreach (string name in Directory.GetFiles(path))
            if (!IsSystemName(name))
                children.Add(name);
    }
    e.Children = children;
}
protected void ASPxTreeList1_VirtualModeNodeCreating(object sender,
TreeListVirtualModeNodeCreatingEventArgs e) {
    string path = e.NodeObject.ToString();

    e.NodeKeyValue = GetNodeGuid(path);
    e.IsLeaf = !Directory.Exists(path);
    e.SetNodeValue("FileName", PopFileName(path));
}

// Helpers

Guid GetNodeGuid(string path) {
    if (!Map.ContainsKey(path))
        Map[path] = Guid.NewGuid();
    return Map[path];
}
Dictionary<string, Guid> Map {
    get {
        const string key = "DX_PATH_GUID_MAP";
        if (Session[key] == null)
            Session[key] = new Dictionary<string, Guid>();
        return Session[key] as Dictionary<string, Guid>;
    }
}
string PopFileName(string path) {
    return path.Substring(1 + path.LastIndexOf("\\"));
}
bool IsSystemName(string name) {
    name = PopFileName(name).ToLower();
    return name.StartsWith("app_") || name == "bin"
        || name.EndsWith(".aspx.cs") || name.EndsWith(".aspx.vb");
}
See Also