aspnet-4008-components-tree-list-concepts-binding-to-data-virtual-mode.md
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.
VirtualModeCreateChildren
VirtualModeNodeCreating
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
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. |
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.
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");
}
Imports System.IO
Imports System.Collections.Generic
Imports DevExpress.Web.ASPxTreeList
Protected Sub ASPxTreeList1_VirtualModeCreateChildren(ByVal sender As Object,_
ByVal e As TreeListVirtualModeCreateChildrenEventArgs)
Dim path As String
If e.NodeObject Is Nothing Then
path = Page.MapPath("~/")
Else
path = e.NodeObject.ToString()
End If
Dim children As List(Of String) = New List(Of String)()
If Directory.Exists(path) Then
For Each name As String In Directory.GetDirectories(path)
If (Not IsSystemName(name)) Then
children.Add(name)
End If
Next name
For Each name As String In Directory.GetFiles(path)
If (Not IsSystemName(name)) Then
children.Add(name)
End If
Next name
End If
e.Children = children
End Sub
Protected Sub ASPxTreeList1_VirtualModeNodeCreating(ByVal sender As Object,_
ByVal e As TreeListVirtualModeNodeCreatingEventArgs)
Dim path As String = e.NodeObject.ToString()
e.NodeKeyValue = GetNodeGuid(path)
e.IsLeaf = Not Directory.Exists(path)
e.SetNodeValue("FileName", PopFileName(path))
End Sub
' Helpers
Private Function GetNodeGuid(ByVal path As String) As Guid
If (Not Map.ContainsKey(path)) Then
Map(path) = Guid.NewGuid()
End If
Return Map(path)
End Function
Private ReadOnly Property Map() As Dictionary(Of String, Guid)
Get
Const key As String = "DX_PATH_GUID_MAP"
If Session(key) Is Nothing Then
Session(key) = New Dictionary(Of String, Guid)()
End If
Return TryCast(Session(key), Dictionary(Of String, Guid))
End Get
End Property
Private Function PopFileName(ByVal path As String) As String
Return path.Substring(1 & path.LastIndexOf("\"))
End Function
Private Function IsSystemName(ByVal name As String) As Boolean
name = PopFileName(name).ToLower()
Return name.StartsWith("app_") OrElse name = "bin" OrElse name.EndsWith(".aspx.cs")_
OrElse name.EndsWith(".aspx.vb")
End Function
See Also
Online Demo: Data Binding - Virtual Mode
KB article: How to access invisible selected nodes in the virtual mode