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

RadComboBox items are not accessible on the server-side when loading them on demand

DESCRIPTION

RadComboBox items loaded on demand using the ItemsRequested event, ASMX WebService or WCF WebService do not exist on the server, therefore they cannot be accessed.

The following properties and methods will not yield results:

  • FindItemByText - returns null / Nothing
  • FindItemByValue - returns null / Nothing
  • SelectedItem - returns null / Nothing
  • SelectedIndex - returns -1

The purpose of the Load On Demand functionality is to boost the loading speed by using little to no ViewState related information.

SOLUTION

The only information that will be persisted is the Text and Value.

If using the SelectedIndexChanged event of the Combo, the Text and Value will be available in the event arguments (e.Text and e.Value)

In other events, you can access the Text and Value through the Text and SelectedValue properties of the RadComboBox instance.

Example Combo Markup

<telerik:RadComboBox ID="RadComboBox1" runat="server" RenderMode="Lightweight" EnableLoadOnDemand="true" 
    AutoPostBack="true"
    OnItemsRequested="RadComboBox1_ItemsRequested"
    OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged">
</telerik:RadComboBox>

Example ItemsRequested event

protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
    RadComboBox combo = (RadComboBox)sender;

    // DataTable with Dummy data
    var dt = new DataTable();
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Columns.Add("Description", typeof(string));

    for (int i = 0; i < 10; i++)
    {
        var newRow = dt.NewRow();

        newRow["ID"] = i;
        newRow["Name"] = "Name " + i;
        newRow["Description"] = "Desc " + i;

        dt.Rows.Add(newRow);
    }

    // Required by the Load On Demand functionality
    // Loop through the data
    foreach (DataRow row in dt.Rows)
    {
        // Create Combo Item and pass the Text as parameter
        RadComboBoxItem item = new RadComboBoxItem();

        // Populate the Text and Value properties
        item.Text = row["Name"].ToString(); // Needed to access the Text
        item.Value = row["ID"].ToString(); // Needed to access the Value

        // add the Combo Item to the Combo's Items collection.
        combo.Items.Add(item);
    }
}
Protected Sub RadComboBox1_ItemsRequested(ByVal sender As Object, ByVal e As RadComboBoxItemsRequestedEventArgs)
    Dim combo As RadComboBox = CType(sender, RadComboBox)

    'DataTable with Dummy data
    Dim dt = New DataTable()
    dt.Columns.Add("ID", GetType(Integer))
    dt.Columns.Add("Name", GetType(String))
    dt.Columns.Add("Description", GetType(String))

    'Required by the Load On Demand functionality
    'Loop through the data
    For i As Integer = 0 To 10 - 1
        Dim newRow = dt.NewRow()
        newRow("ID") = i
        newRow("Name") = "Name " & i
        newRow("Description") = "Desc " & i
        dt.Rows.Add(newRow)
    Next

    For Each row As DataRow In dt.Rows
        'Create Combo Item and pass the Text as parameter
        Dim item As RadComboBoxItem = New RadComboBoxItem()

        'Populate the Text and Value properties
        item.Text = row("Name").ToString() 'Needed to access the Text
        item.Value = row("ID").ToString() 'Needed to access the Value

        'add the Combo Item to the Combo's Items collection.
        combo.Items.Add(item)
    Next
End Sub

Accessing the Text and Value on server side using the Combo's SelectedIndexChanged event

protected void RadComboBox1_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
    var text = e.Text;
    var value = e.Value;
    // OR 
    var combo = (RadComboBox)sender;
    var text2 = combo.Text;
    var value2 = combo.SelectedValue;
}
Protected Sub RadComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As RadComboBoxSelectedIndexChangedEventArgs)
    Dim text = e.Text
    Dim value = e.Value
    'OR 
    Dim combo = CType(sender, RadComboBox)
    Dim text2 = combo.Text
    Dim value2 = combo.SelectedValue
End Sub

Accessing the Text and Value on server side in a Button Click

protected void RadButton1_Click(object sender, EventArgs e)
{
    var text = RadComboBox1.Text;
    var selectedValue = RadComboBox1.SelectedValue;
}
Protected Sub RadButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim text = RadComboBox1.Text
    Dim selectedValue = RadComboBox1.SelectedValue
End Sub

See Also

In this article