Back to Devexpress

ASPxTreeList.VirtualModeNodeCreating Event

aspnet-devexpress-dot-web-dot-aspxtreelist-dot-aspxtreelist-a2d842cf.md

latest7.3 KB
Original Source

ASPxTreeList.VirtualModeNodeCreating Event

Enables you to initialize nodes in a tree virtual mode.

Namespace : DevExpress.Web.ASPxTreeList

Assembly : DevExpress.Web.ASPxTreeList.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event TreeListVirtualModeNodeCreatingEventHandler VirtualModeNodeCreating
vb
Public Event VirtualModeNodeCreating As TreeListVirtualModeNodeCreatingEventHandler

Event Data

The VirtualModeNodeCreating event's data class is TreeListVirtualModeNodeCreatingEventArgs. The following properties provide information specific to this event:

PropertyDescription
IsLeafGets or sets whether the processed node has zero child nodes.
NodeKeyValueGets or sets the processed node’s key value.
NodeObjectGets the object that corresponds to the processed node in a tree.

The event data class exposes the following methods:

MethodDescription
SetNodeValue(String, Object)Assigns a value to the specified cell.

Remarks

In virtual mode a tree is created on demand. In this instance, child nodes are created and initialized when their parent node is expanded.

The ASPxTreeList fires two events that you should process in code. These include:

The VirtualModeNodeCreating event is raised for each node object created within the ASPxTreeList.VirtualModeCreateChildren event handler. This event enables you to initialize the corresponding node in a tree. You should specify the node’s key value and cell values. If you are sure that the processed node has no child nodes, set the event parameter’s TreeListVirtualModeNodeCreatingEventArgs.IsLeaf property to true.

For detailed information, see Virtual Mode.

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.

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

VirtualModeCreateChildren

Virtual Mode

Tree List

ASPxTreeList Class

ASPxTreeList Members

DevExpress.Web.ASPxTreeList Namespace