Back to Devexpress

TreeViewExtension.BindToVirtualData(TreeViewVirtualModeCreateChildrenMethod) Method

aspnetmvc-devexpress-dot-web-dot-mvc-dot-treeviewextension-dot-bindtovirtualdata-x28-devexpress-dot-web-dot-mvc-dot-treeviewvirtualmodecreatechildrenmethod-x29.md

latest5.5 KB
Original Source

TreeViewExtension.BindToVirtualData(TreeViewVirtualModeCreateChildrenMethod) Method

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

Namespace : DevExpress.Web.Mvc

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

NuGet Package : DevExpress.Web.Mvc5

Declaration

csharp
public TreeViewExtension BindToVirtualData(
    TreeViewVirtualModeCreateChildrenMethod method
)
vb
Public Function BindToVirtualData(
    method As TreeViewVirtualModeCreateChildrenMethod
) As TreeViewExtension

Parameters

NameTypeDescription
methodTreeViewVirtualModeCreateChildrenMethod

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

TypeDescription
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

csharp
// 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";
        }
}
csharp
// View code

@Html.DevExpress().TreeView(
     settings =>
     {
          settings.Name = "tvVirtualMode";
          settings.CallbackRouteValues = new { Controller = "TreeView", Action = "VirtualModePartial" };
          settings.Images.NodeImage.Width = 13;
          settings.Styles.NodeImage.Paddings.PaddingTop = 3;
     }).BindToVirtualData(TreeViewVirtualModeHelper.CreateChildren).GetHtml()

See Also

TreeView

Virtual Mode

Online Demo: Tree View - Virtual Mode

TreeViewExtension Class

TreeViewExtension Members

DevExpress.Web.Mvc Namespace