Skip to main content
A newer version of this page is available. .

TreeViewExtension.BindToVirtualData(TreeViewVirtualModeCreateChildrenMethod) Method

Allows creation of treeview nodes on demand (virtual mode).

Namespace: DevExpress.Web.Mvc

Assembly: DevExpress.Web.Mvc5.v18.2.dll

Declaration

public TreeViewExtension BindToVirtualData(
    TreeViewVirtualModeCreateChildrenMethod method
)

Parameters

Name Type Description
method TreeViewVirtualModeCreateChildrenMethod

A delegate method of the TreeViewVirtualModeCreateChildrenMethod type that enables you to create a list of business objects that correspond to the child nodes owned by the processed node.

Returns

Type Description
TreeViewExtension

A TreeViewExtension object that is the TreeView.

Remarks

In addition to bound and unbound modes, the TreeView extension can operate in a 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 is created on demand. In this instance, child nodes are created and initialized when their parent node is expanded.

To implement a virtual mode for the TreeView extension, you should use a specifically parameterized BindToVirtualData method. The method’s parameter refers to the delegate method that can be declared as a static method within a model class. Within this delegate method, create a list of business objects that correspond to the child nodes owned by the processed node.

Note

The TreeViewSettings.SyncSelectionMode property affects the TreeView extension behavior in virtual mode. To learn more see the Virtual Mode topic.

Example

// Model (TreeViewVirtualModeHelper)

public class TreeViewVirtualModeHelper {
        const string FileImageUrl = "~/Content/TreeView/FileSystem/file.png";
        const string DirImageUrl = "~/Content/TreeView/FileSystem/directory.png";
        static HttpRequest Request { get { return HttpContext.Current.Request; } }

        public static void CreateChildren(TreeViewVirtualModeCreateChildrenEventArgs e) {
            string parentNodePath = string.IsNullOrEmpty(e.NodeName) ? Request.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;
        }

        static bool IsSystemName(string name) {
            name = name.ToLower();
            return name.StartsWith("app_") || name == "bin" || name == "obj";
        }
}
See Also