Binding to ASP.NET Datasource Components
RadListBox supports binding to all ASP.NET 2.0/3.5 DataSource components, including
-
AccessDataSource
-
SqlDataSource
-
XmlDataSource
-
ObjectDataSource
-
LinqDataSource
-
EntityDataSource
-
OpenAccessDataSource
To bind to a DataSource component, all you need to do is set the DataSourceID property of the ListBox to the ID of the DataSource component. You should also set the DataTextField and DataValueField properties of the ListBox to map the Text and Value properties of the items to the respective columns/fields from the data source.
If you need to map additional columns from the data source to properties of the ListBox items, you can use the ItemDataBound event. The event arguments passed to the event, e.Item and e.Item.DataItem, hold the instance of the ListBox item being bound and the DataItem to which it is bound. You can map a property from the DataItem to the corresponding property of the ListBox item. Be sure to cast the DataItem object to the proper data type first.
protected void RadListBox1_ItemDataBound(object sender, RadListBoxItemEventArgs e)
{
DataRowView dataSourceRow = (DataRowView)e.Item.DataItem;
//setting different properties for the items
e.Item.Enabled = (bool)dataSourceRow["Enabled"];
e.Item.ToolTip = dataSourceRow["ProductName"].ToString();
e.Item.Attributes["City"] = dataSourceRow["CityColumn"].ToString();
}
AccessDataSource
<telerik:RadListBox RenderMode="Lightweight" ID="RadListBox1"
runat="server"
DataTextField="Name"
DataValueField="ID"
DataSourceID="AccessDataSource1">
</telerik:RadListBox>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Countries.mdb"
SelectCommand="SELECT [ID], [Name] FROM [Countries]">
</asp:AccessDataSource>
SqlDataSource
<telerik:RadListBox RenderMode="Lightweight" ID="RadListBox1"
runat="server"
DataTextField="ProductName"
DataValueField="ProductID"
DataSourceID="SqlDataSource1">
</telerik:RadListBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<$ ConnectionStrings>"
SelectCommand="SELECT [ProductID], [ProductName] FROM [Products]">
</asp:SqlDataSource>
XmlDataSource
<telerik:RadListBox RenderMode="Lightweight" ID="RadListBox1"
runat="server"
DataTextField="Text"
DataValueField="Value"
DataSourceID="XmlDataSource1">
</telerik:RadListBox>
<asp:XmlDataSource ID="XmlDataSource1"
runat="server"
DataFile="~/ListBox.xml">
</asp:XmlDataSource>
When using an XmlDataSource, the XML file should have the format shown below:
<Items>
<Item Text="New York" Value="1" />
<Item Text="Paris" Value="2" />
<Item Text="London" Value="3" />
</Items>
ObjectDataSource
<telerik:RadListBox RenderMode="Lightweight" ID="RadListBox1"
runat="server"
DataTextField="Text"
DataValueField="ID"
DataSourceID="ObjectDataSource1">
</telerik:RadListBox>
<asp:ObjectDataSource ID="ObjectDataSource1"
TypeName="ListObjectData"
SelectMethod="GetItems"
runat="server">
</asp:ObjectDataSource>
public class ListObjectData
{
public ListObjectData()
{
}
public static List<ListDataItem> GetItems()
{
List<ListDataItem> itemsList = new List<ListDataItem>();
itemsList.Add(new ListDataItem(1, "item 1"));
itemsList.Add(new ListDataItem(2, "item 2"));
itemsList.Add(new ListDataItem(3, "item 3"));
return itemsList;
}
public class ListDataItem
{
private string _text;
private int _id;
public string Text
{
get { return _text; }
set { _text = value; }
}
public int ID
{
get { return _id; }
set { _id = value; }
}
public ListDataItem(int id, string text)
{
_id = id;
_text = text;
}
}
}
LinqDataSource
<telerik:RadListBox RenderMode="Lightweight" ID="RadListBox1"
runat="server"
DataTextField="ProductName"
DataValueField="ProductID"
DataSourceID="LinqDataSource1">
</telerik:RadListBox>
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="NorthwindDataContext"
Select="new (ProductID, ProductName)"
TableName="Products">
</asp:LinqDataSource>
EntityDataSource
<telerik:RadListBox RenderMode="Lightweight" runat="server" ID="RadListBox1" Height="200px" Width="200px"
DataSourceID="EntityDataSource1" DataTextField="ContactName">
</telerik:RadListBox>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=NorthwindEntities"
DefaultContainerName="NorthwindEntities"
EntitySetName="Customers"
Select="it.[ContactName], it.[City], it.[ContactTitle]"
AutoPage="true" OrderBy="it.[ContactName]">
</asp:EntityDataSource>