Working with Items at Server-Side
Using the server-side API, you can programmatically add, remove, disable, or select items in RadComboBox.
Adding items
Use the Add method of the RadComboBoxItemCollection object to add items programmatically at Page_Load, Button_Click or another server event handler:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
RadComboBoxItem item1 = new RadComboBoxItem();
item1.Text = "Item1";
item1.Value = "1";
RadComboBox1.Items.Add(item1);
RadComboBoxItem item2 = new RadComboBoxItem();
item2.Text = "Item2";
item2.Value = "2";
RadComboBox1.Items.Add(item2);
RadComboBoxItem item3 = new RadComboBoxItem();
item3.Text = "Item3";
item3.Value = "3";
RadComboBox1.Items.Add(item3);
}
}
Removing items
Use the Remove method of the RadComboBoxItemCollection object to remove items:
//Find the desired item and remove it.
RadComboBoxItem item = RadComboBox1.FindItemByText("Item1");
item.Remove();
//Remove an item from the Items collection. The code line below removes the first item. Its index is 0.
RadComboBox1.Items.Remove(0);
Disabling items
Use the Enable property of the RadComboBoxItem object to enable or disable an item:
//Find the desired item and disable it.
RadComboBoxItem item = RadComboBox1.FindItemByText("Item1");
item.Enabled = false;
Selecting Items
Use the Selected property of an item to select it, or use the SelectedIndex property of RadComboBox:
//Use RadComboBoxItem.Selected
RadComboBoxItem item = RadComboBox1.FindItemByText("Item 2");
item.Selected = true;
//Use RadComboBox.SelectedIndex
int index = RadComboBox1.FindItemIndexByValue("2");
RadComboBox1.SelectedIndex = index;
//You can also use the SelectedValue property.
RadComboBox1.SelectedValue = value;
Finding Items
You can locate by searching on the Text or Value properties. You can retrieve either the index of the item or a reference to the item itself. Use the RadComboBox methods:
-
FindItemByText
-
FindItemByValue
-
FindItemIndexByText
-
FindItemIndexByValue
using System;
using Telerik.Web.UI;
namespace RadWindow
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RadComboBox1.Items.Add(new RadComboBoxItem("One", "1"));
RadComboBox1.Items.Add(new RadComboBoxItem("Two", "2"));
RadComboBox1.Items.Add(new RadComboBoxItem("Three", "3"));
}
protected void Button1_Click(object sender, EventArgs e)
{
RadComboBoxItem item = null;
int index = -1;
//Get a reference to a RadComboBoxItem with Text property = "Two".
item = RadComboBox1.FindItemByText("Two");
//Get a reference to a RadComboBoxItem with Value property = "2".
item = RadComboBox1.FindItemByValue("2");
//Get the index of a RadComboBoxItem with Text property = "Two".
index = RadComboBox1.FindItemIndexByText("Two");
item = RadComboBox1.Items[index];
//Get the index of a RadComboBoxItem with Value property = "2".
index = RadComboBox1.FindItemIndexByValue("2");
item = RadComboBox1.Items[index];
}
}
}
For a live example, see: Add/Remove/Disable Items