RadComboBox for ASP.NET

When is the ClientDataString needed? Send comments on this topic.
Load On Demand Support > When is the ClientDataString needed?

Glossary Item Box

The ClientDataString property provides a flexible way to pass information from the client-side to the server.

The most common scenario when the ClientDataString property comes to help is the following:

 

The ClientDataString property has to be set in the OnClientItemsRequesting client-side event!


  • Multiple comboboxes on the same page. All comboboxes have the EnableLoadOnDemand property set to true and the items are added via callback.
  • The comboboxes are dependant - the items in the second combobox are loaded according to what is selected in the first combobox.
  • You need to get the selected text/value of the first combobox in the ItemsRequested event of the second combobox

First, you should check the following article to understand how to get the text or value (on the server-side) of the selected item when the items are loaded on demand (via callback):
Getting the text or value of the selected item in the ItemsRequested event.

Example:

ASPX Copy Code
<script language="javascript" type="text/javascript">
 
function GetSelectedItem(combobox)
 {
    //get the text of the selected item in the first (countries) combobox
    var countriesCombo =
<%=Countries.ClientID %>;
    var countriesText = countriesCombo.GetText();
    //set the text of the countries combobox to the ClientDataString property of the second (cities) combo
    combobox.ClientDataString = countriesText;
    //then, you will be able to get the text of the countries combo in the ItemsRequested event of the second (cities) combobox (see the C#, VB.NET examples below)
       }
</script>

<
rad:RadComboBox
   
ID="Countries"
   
runat="server"
   
EnableLoadOnDemand="true"
   
OnItemsRequested="Countries_ItemsRequested">
</
rad:RadComboBox>
<
rad:RadComboBox
   
ID="Cities"
   
runat="server"
   
EnableLoadOnDemand="true"
   
OnItemsRequested="Cities_ItemsRequested"
   
OnClientItemsRequesting="GetSelectedItem">
</
rad:RadComboBox>
C# Copy Code
protected void Countries_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
   {
       Countries.Items.Add(
new RadComboBoxItem("USA", "USA"));
       Countries.Items.Add(
new RadComboBoxItem("UK", "UK"));
       Countries.Items.Add(
new RadComboBoxItem("Germany", "Germany"));
   }
protected void Cities_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
{
   
/*you need to check which item is selected in the first combobox.
    * Since Countries.Text is empty, you should use the ClientDataString property.
    * The ClientDataString property has been previously set on the client-side*/
   
if (e.ClientDataString == "UK")
   {
       Cities.Items.Add(
new RadComboBoxItem("London", "London"));
       Cities.Items.Add(
new RadComboBoxItem("Oxford", "Oxford"));
   }
}
VB.NET Copy Code
Protected Sub Countries_ItemsRequested(o As Object, e As RadComboBoxItemsRequestedEventArgs)
   Countries.Items.Add(New RadComboBoxItem("USA", "USA"))
   Countries.Items.Add(New RadComboBoxItem("UK", "UK"))
   Countries.Items.Add(New RadComboBoxItem("Germany", "Germany"))
End Sub 'Countries_ItemsRequested
Protected Sub Cities_ItemsRequested(o As Object, e As RadComboBoxItemsRequestedEventArgs)
   'you need to check which item is selected in the first combobox.
   'Since Countries.Text is empty, you should use the ClientDataString property.
   'The ClientDataString property has been set on the client-side
   If e.ClientDataString = "UK" Then
      Cities.Items.Add(New RadComboBoxItem("London", "London"))
      Cities.Items.Add(New RadComboBoxItem("Oxford", "Oxford"))
   End If
End Sub 'Cities_ItemsRequested