RadControls for ASP.NET AJAX
Instead of supplying a NodeExpand event handler to add nodes on-demand to the treeview, you can let the treeview call on a WebService to service the request. The path to the web service and the name of the service method are specified in the WebServiceSettingsPath and Method properties:
CopyASPX
<telerik:radtreeview id="RadTreeView1" runat="server" onclientnodepopulating="nodePopulating">
<Nodes>
<telerik:RadTreeNode runat="server" Text="Root RadTreeNode1" ExpandMode="WebService"
Value="1">
</telerik:RadTreeNode>
<telerik:RadTreeNode runat="server" Text="Root RadTreeNode2" ExpandMode="WebService"
Value="2">
</telerik:RadTreeNode>
</Nodes>
<WebServiceSettings Path="ProductCategories.asmx" Method="GetTreeViewCategories" />
</telerik:radtreeview>
<script type="text/javascript">
function nodePopulating(sender, eventArgs) {
var node = eventArgs.get_node();
var context = eventArgs.get_context();
context["CategoryID"] = node.get_value();
}
</script>
The HTTP method by default is POST. To change that you can set the UseHttpGet property of the WebServiceSettings to True. This property is added in Q1 2009 release.
To use the integrated support, the Web service should have the signature as shown in the code example below.
Note |
|---|
Notice in the code below:
The ScriptService attribute marks the ProductCategories class and makes the web method callable from client-side script and therefore available to AJAX.
The signature of the web method includes parameters for a RadTreeNodeData and an object. The RadTreeNodeData parameter represents the parent node being populated. The second parameter is an object that contains context information. Cast the object as IDictionary as shown in the example below. You can then retrieve values that have been placed in the dictionary earlier in the OnClientNodePopulating client event.
The array of RadTreeNodeData returned by the method is used to store the data that will populate the child nodes. Create and assign properties to the RadTreeNodeData array members just as you would to the actual child nodes.
|
CopyC#
using System.Collections.Generic;
using System.Web.Script.Services;
using System.Web.Services;
using Telerik.Web.UI;
[ScriptService]
public class ProductCategories : WebService
{
[WebMethod]
public RadTreeNodeData[] GetTreeViewCategories(RadTreeNodeData node, object context)
{
IDictionary<string, object> contextDictionary = (IDictionary<string, object>)context;
List<RadTreeNodeData> result = new List<RadTreeNodeData>();
RadTreeNodeData nodeData = new RadTreeNodeData();
nodeData.Text = "child" + contextDictionary["CategoryID"] + i;
nodeData.Value = contextDictionary["CategoryID"].ToString() + i;
result.Add(nodeData);
return result.ToArray();
}
}
CopyVB.NET
Imports System.Collections.Generic
Imports System.Web.Script.Services
Imports System.Web.Services
Imports Telerik.Web.UI
<ScriptService()> _
Public Class ProductCategories
Inherits WebService
<WebMethod()> _
Public Function GetTreeViewCategories(ByVal node As RadTreeNodeData, ByVal context As Object) As RadTreeNodeData()
Dim contextDictionary As IDictionary(Of String, Object) = DirectCast(context, IDictionary(Of String, Object))
Dim result As New List(Of RadTreeNodeData)()
Dim nodeData As New RadTreeNodeData()
nodeData.Text = "child" + contextDictionary("CategoryID") + i
nodeData.Value = contextDictionary("CategoryID").ToString() + i
result.Add(nodeData)
Return result.ToArray()
End Function
End Class
For a live example of using a Web service to populate a RadTreeView, see Populate From Web Service.