Back to Devexpress

ASPxTreeView.VirtualModeCreateChildren Event

aspnet-devexpress-dot-web-dot-aspxtreeview-8a7f41d5.md

latest12.1 KB
Original Source

ASPxTreeView.VirtualModeCreateChildren Event

Used to activate virtual mode. Occurs when expanding a node for the first time in this mode.

Namespace : DevExpress.Web

Assembly : DevExpress.Web.v25.2.dll

NuGet Package : DevExpress.Web

Declaration

csharp
public event TreeViewVirtualModeCreateChildrenEventHandler VirtualModeCreateChildren
vb
Public Event VirtualModeCreateChildren As TreeViewVirtualModeCreateChildrenEventHandler

Event Data

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

PropertyDescription
ChildrenGets or sets the collection of children for the currently processed node object.
NodeNameGets or sets the processed node’s name.

Remarks

In virtual mode, a tree is created on the server in portions on a client request, at start-up, and when expanding nodes. So, in virtual mode, child nodes are not created and initialized until their parent node is expanded for the first time.

Run Demo: Tree View - Virtual Mode

Note

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

  • When the ASPxTreeView.SyncSelectionMode property is set to None , the ASPxTreeView.VirtualModeCreateChildren event only fires for the node being expanded.
  • Otherwise, the ASPxTreeView.VirtualModeCreateChildren event fires for the node being expanded, and for all previously expanded nodes.

Handle the VirtualModeCreateChildren event to activate virtual mode for the ASPxTreeView. Within the event handler, you need to create a list of TreeViewVirtualNode objects specifying child nodes for the currently expanded node. If a child node has no children, set its TreeViewVirtualNode.IsLeaf property to true, to prevent showing the expand button for this node.

Note

You can set a child node’s TreeViewVirtualNode.Expanded property to true directly within a VirtualModeCreateChildren event handler to fire the event for this node. That way, you can expand only specific nodes or recursively expand nodes to a specific nesting level. To distinguish child nodes or their nesting levels, assign node names (TreeViewVirtualNode.Name property values) based on a parent node’s name (TreeViewVirtualModeCreateChildrenEventArgs.NodeName).

Example 1

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

Example 2

The following code snippet demonstrates how to handle the VirtualModeCreateChildren event to add child nodes based on the nesting level. Two child nodes are created for the root node (these child nodes are labeled ‘Parent Node1’ and ‘Parent Node2’) and nodes at the next nesting level only (these child nodes are labeled ‘Child Node1’ and ‘Child Node2’).

The initial node hierarchy will appear as follows.

csharp
// ...
    const string InitialNodeNamePrefix = "InitialNode";
    protected void ASPxTreeView1_VirtualModeCreateChildren(object source, DevExpress.Web.TreeViewVirtualModeCreateChildrenEventArgs e) {
        List<TreeViewVirtualNode> nodes = new List<TreeViewVirtualNode>();

        // Populating nodes based on the currently processed parent node
        // identified by e.NodeName.
        if (e.NodeName == null)
        {
            // Processing the root node.
            TreeViewVirtualNode initialNode1 = new TreeViewVirtualNode(InitialNodeNamePrefix + "1", "Parent Node1");
            initialNode1.Expanded = true;
            nodes.Add(initialNode1);
            TreeViewVirtualNode initialNode2 = new TreeViewVirtualNode(InitialNodeNamePrefix + "2", "Parent Node2");
            initialNode2.Expanded = true;
            nodes.Add(initialNode2);
        }
        else
        {
            if (e.NodeName.StartsWith(InitialNodeNamePrefix))
            {
                // Creating a unique child node name based on its parent node name.
                TreeViewVirtualNode childNode1 = new TreeViewVirtualNode("ChildNode1" + e.NodeName, "Child Node1");
                nodes.Add(childNode1);

                // Creating a unique child node name based on its parent node name.
                TreeViewVirtualNode childNode2 = new TreeViewVirtualNode("ChildNode2" + e.NodeName, "Child Node2");
                nodes.Add(childNode2);
            }
            else
            {
                // Other child nodes are created as follows.
                TreeViewVirtualNode childNode = new TreeViewVirtualNode("Child" + e.NodeName, "Another Child Node");
                nodes.Add(childNode);
            }
        }
        e.Children = nodes;
    }
vb
' ...
    Private Const InitialNodeNamePrefix As String = "InitialNode"
    Protected Sub ASPxTreeView1_VirtualModeCreateChildren(ByVal source As Object, ByVal e As DevExpress.Web.TreeViewVirtualModeCreateChildrenEventArgs)
        Dim nodes As New List(Of TreeViewVirtualNode)()

        ' Populating nodes based on the currently processed parent node
        ' identified by e.NodeName.
        If e.NodeName Is Nothing Then
            ' Processing the root node.
            Dim initialNode1 As New TreeViewVirtualNode(InitialNodeNamePrefix & "1", "Parent Node1")
            initialNode1.Expanded = True
            nodes.Add(initialNode1)
            Dim initialNode2 As New TreeViewVirtualNode(InitialNodeNamePrefix & "2", "Parent Node2")
            initialNode2.Expanded = True
            nodes.Add(initialNode2)
        Else
            If e.NodeName.StartsWith(InitialNodeNamePrefix) Then
                ' Creating a unique child node name based on its parent node name.
                Dim childNode1 As New TreeViewVirtualNode("ChildNode1" & e.NodeName, "Child Node1")
                nodes.Add(childNode1)

                ' Creating a unique child node name based on its parent node name.
                Dim childNode2 As New TreeViewVirtualNode("ChildNode2" & e.NodeName, "Child Node2")
                nodes.Add(childNode2)
            Else
                ' Other child nodes are created as follows.
                Dim childNode As New TreeViewVirtualNode("Child" & e.NodeName, "Another Child Node")
                nodes.Add(childNode)
            End If
        End If
        e.Children = nodes
    End Sub

See Also

Virtual Mode

Tree View

ASPxTreeView Class

ASPxTreeView Members

DevExpress.Web Namespace