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.v24.1.dll
NuGet Package: DevExpress.Web
Declaration
Event Data
The VirtualModeCreateChildren event's data class is TreeViewVirtualModeCreateChildrenEventArgs. The following properties provide information specific to this event:
Property | Description |
---|---|
Children | Gets or sets the collection of children for the currently processed node object. |
NodeName | Gets 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.
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.
<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");
}
}
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.
// ...
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;
}