Binding to RadClientDataSource
This article demonstrates how to bind RadListBox to RadClientDataSource.
Since Q2 2014 RadListBox can be bound to a RadClientDataSource control. An important aspect of binding to the RadClientDataSource is that the RadListBox DataKeyField, DataText and DataValue fields should be associated with the custom object properties. Thus you can choose which property value to be shown as RadListBox item text and value. For reference at the bottom of the web service implementation below you will find the custom class and its properties declaration.
The following application scenario shows an example of such scenario.
When the LoadOnDemand functionality is enabled RadListBox requests items from the RadClientDataSource on portions.In case that this functionality is disabled the items are request at once.
<telerik:RadClientDataSource runat="server" ID="CD1">
<ClientEvents />
<DataSource>
<WebServiceDataSourceSettings BaseUrl="http://url/Service.svc/">
<Select Url="LoadCustomDataWCF" DataType="JSON" RequestType="Post" ContentType="application/json; charset=utf-8" />
</WebServiceDataSourceSettings>
</DataSource>
<Schema DataName="d">
</Schema>
<ClientEvents />
</telerik:RadClientDataSource>
<br />
<telerik:RadListBox RenderMode="Lightweight" Height="100px" TabIndex="1" runat="server" ID="ListBox1" ClientDataSourceID="CD1"
DataTextField="Name" DataValueField="Value" EnableLoadOnDemand="true">
</telerik:RadListBox>
[OperationContract]
public CustomData[] LoadCustomDataWCF()
{
NorthwindReadOnlyDataContext northwind = new NorthwindReadOnlyDataContext();
//Get all items from the Customers table. This query will not be executed untill the ToArray method is called.
var allCustomers = (from customer in northwind.Customers
orderby customer.ContactName
select new CustomData
{
Name = customer.ContactName,
Value = customer.CustomerID
}).Take<CustomData>(46);
var result = allCustomers.ToArray();
return result;
}
public class CustomData
{
public string Name { get; set; }
public string Value { get; set; }
}