New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Binding to Collections
Programmatic Data Binding on Server-side involves setting the DataSource
property and calling the DataBind
method. This can be done either in the Init
or Load
events of the Page's LifeCycle.
Binding to Array, ArrayList
Markup
ASPNET
<telerik:radcombobox id="RadComboBox1" runat="server" />
<telerik:radcombobox id="RadComboBox2" runat="server" />
CodeBehind
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindToArrayList(RadComboBox1);
BindToArray(RadComboBox2);
}
}
private void BindToArray(RadComboBox combo)
{
string[] itemsList = { "One", "Two", "Three" };
combo.DataSource = itemsList;
combo.DataBind();
}
private void BindToArrayList(RadComboBox combo)
{
ArrayList itemsList = new ArrayList();
itemsList.Add("One");
itemsList.Add("Two");
itemsList.Add("Three");
combo.DataSource = itemsList;
combo.DataBind();
}
Binding to DataTable, DataSet, DataView
In case of structured data source, you will also need to set the DataTextField
and DataValueField
properties.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadComboBox1.DataValueField = "ProductID";
RadComboBox1.DataTextField = "ProductName";
RadComboBox1.DataSource = GetSampleSource();
RadComboBox1.DataBind();
}
}
private DataTable GetSampleSource()
{
DataTable dataTable = new DataTable();
DataColumn column = new DataColumn();
column.DataType = Type.GetType("System.Int32");
column.ColumnName = "ProductID";
dataTable.Columns.Add(column);
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "ProductName";
dataTable.Columns.Add(column);
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = dataTable.Columns["ProductID"];
dataTable.PrimaryKey = PrimaryKeyColumns;
for (int i = 0; i <= 6; i++)
{
DataRow row = dataTable.NewRow();
row["ProductID"] = i + 1;
row["ProductName"] = "Product " + (i + 1);
dataTable.Rows.Add(row);
}
return dataTable;
}