Callbacks
- 2 minutes to read
The MVC TreeView supports AJAX with the ability to load the content of parent nodes from the server via callbacks. This approach allows the TreeView to avoid the initial transfer of all node data to the client, thus improving page load times.
In order to activate the use of callbacks, the TreeViews’s TreeViewSettings.CallbackRouteValues property should contain the path to an action that handles a callback request. The best way to handle such callbacks is to use a partial view that only contains a TreeView. In this instance, the contents of the collapsed nodes are not represented on the client. When a parent node is expanded for the first time, its content is retrieved from the server and then cached on the client. The next time the node is expanded, its content is taken from the client and no callback to the server is performed.
Examples
The sample below demonstrates the use of the AJAX-based callbacks implemented by the MVC TreeView. The callback processing is paused on the server side, so that the Loading Panel is visible during processing.
Controller code:
using System.Threading;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
namespace DevExpress.Web.Demos {
public partial class TreeViewController: DemoController {
public ActionResult Callbacks() {
return DemoView("Callbacks");
}
public ActionResult CallbacksPartial() {
if(DevExpressHelper.IsCallback)
Thread.Sleep(1000);
return PartialView("CallbacksPartial");
}
}
}
View code (ASPX):
<%
Html.DevExpress().TreeView(
settings =>
{
settings.Name = "tvCallbacks";
settings.CallbackRouteValues = new { Controller = "TreeView", Action = "CallbacksPartial" };
settings.PreRender = (source, e) => {
ASPxTreeView treeView = (ASPxTreeView)source;
treeView.Nodes[0].Expanded = true;
treeView.Nodes[1].Expanded = true;
};
})
.BindToSiteMap("~/App_Data/Feature.sitemap", false)
.Render();
%>
View code (Razor):
@Html.DevExpress().TreeView(
settings =>
{
settings.Name = "tvCallbacks";
settings.CallbackRouteValues = new { Controller = "TreeView", Action = "CallbacksPartial" };
settings.PreRender = (source, e) => {
ASPxTreeView treeView = (ASPxTreeView)source;
treeView.Nodes[0].Expanded = true;
treeView.Nodes[1].Expanded = true;
};
}).BindToSiteMap("~/App_Data/Feature.sitemap", false).GetHtml()