RadControls for ASP.NET AJAX
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:
CopyC#
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);
}
}
CopyVB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim item1 As New RadComboBoxItem()
item1.Text = "Item1"
item1.Value = "1"
RadComboBox1.Items.Add(item1)
Dim item2 As New RadComboBoxItem()
item2.Text = "Item2"
item2.Value = "2"
RadComboBox1.Items.Add(item2)
Dim item3 As New RadComboBoxItem()
item3.Text = "Item3"
item3.Value = "3"
RadComboBox1.Items.Add(item3)
End If
End Sub
Removing items
Use the Remove method of the RadComboBoxItemCollection object to remove items:
CopyC#
RadComboBoxItem item = RadComboBox1.FindItemByText("Item1");
item.Remove();
RadComboBox1.Items.Remove(0);
CopyVB.NET
Dim item As RadComboBoxItem = RadComboBox1.FindItemByText("Item1")
item.Remove()
RadComboBox1.Items.Remove(0)
Disabling items
Use the Enable property of the RadComboBoxItem object to enable or
disable an item:
CopyC#
RadComboBoxItem item = RadComboBox1.FindItemByText("Item1");
item.Enabled = false;
CopyVB.NET
Dim item As RadComboBoxItem = 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:
CopyC#
RadComboBoxItem item = RadComboBox1.FindItemByText("Item 2");
item.Selected = true;
int index = RadComboBox1.FindItemIndexByValue("2");
RadComboBox1.SelectedIndex = index;
RadComboBox1.SelectedValue = value;
CopyVB.NET
Dim item As RadComboBoxItem = RadComboBox1.FindItemByText("Item 2")
item.Selected = True
Dim index As Integer = RadComboBox1.FindItemIndexByValue("2")
RadComboBox1.SelectedIndex = index
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
CopyC#
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;
item = RadComboBox1.FindItemByText("Two");
item = RadComboBox1.FindItemByValue("2");
index = RadComboBox1.FindItemIndexByText("Two");
item = RadComboBox1.Items[index];
index = RadComboBox1.FindItemIndexByValue("2");
item = RadComboBox1.Items[index];
}
}
}
CopyVB.NET
Imports System
Imports Telerik.Web.UI
namespace RadWindow
Public Partial Class _Default Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
RadComboBox1.Items.Add(New RadComboBoxItem("One", "1"))
RadComboBox1.Items.Add(New RadComboBoxItem("Two", "2"))
RadComboBox1.Items.Add(New RadComboBoxItem("Three", "3"))
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim item As RadComboBoxItem = Nothing
Dim index As Integer = -1
item = RadComboBox1.FindItemByText("Two")
item = RadComboBox1.FindItemByValue("2")
index = RadComboBox1.FindItemIndexByText("Two")
item = RadComboBox1.Items(index)
index = RadComboBox1.FindItemIndexByValue("2")
item = RadComboBox1.Items(index)
End Sub
End Class
End Namespace
For a live example, see: Add/Remove/Disable Items
See Also