New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Binding to DataTable, DataSet or DataView

RadListBox can be bound to a DataTable, DataSet and a DataView. This example shows binding to a DataTable object.

The declaration of the RadListBox object includes no DataSourceID property or section:

<telerik:radlistbox id="RadListBox1" runat="server"></telerik:radlistbox>

In the Page_Load event handler, create and fill the DataTable object, then bind it to the ListBox. You must call the DataBind method after setting the DataSource property.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        RadListBox1.DataSource = GetData(); 
        RadListBox1.DataTextField = "ProductName"; 
        RadListBox1.DataValueField = "ProductID"; 
        RadListBox1.DataBind();
    }
}
protected DataTable GetData()
{
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT ProductID, ProductName FROM Products", ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
    DataTable result = new DataTable(); adapter.Fill(result);
    return result;
}           
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
        RadListBox1.DataSource = GetData()
        RadListBox1.DataTextField = "ProductName"
        RadListBox1.DataValueField = "ProductID"
        RadListBox1.DataBind()
    End If
End Sub
Protected Function GetData() As DataTable
    Dim adapter As New SqlDataAdapter("SELECT ProductID, ProductName FROM Products", ConfigurationManager.ConnectionStrings("NorthwindConnectionString").ConnectionString)
    Dim result As New DataTable()
    adapter.Fill(result)
    Return result

End Function

See Also

In this article