Telerik ASP.NET MVC – Count the number of elements in a Combobox / DropDownList or AutoComplete UI component

Quick Answer

 

Intro

I had this desperately annoying problem where I had two Combobox components. Using the .CascadeTo functionality, I was able to have the second Combobox load data via an AJAX call based on the selection in the first Combobox. This is standard functionality detailed in the demo site here.

The problem was that if the AJAX call to load the second comboxbox with data returned no elements, the Combobox remained disabled. This was unintuitive for the user and made it look like the UI had frozen.

The solution:

  1. Add an ‘OnDataBound’ event handler to the Combobox then from this…
  2. …. count the number of elements in the Combobox and do something to notify the user if there are no elements

Add a ‘OnDataBound’ event handler

This was easily acheived from reading the documentation on Telerik here.

The name of the JavaScript function to be called is ‘RequestorComboBoxDataBound’. This function is called once the data for this Combobox is loaded. This then leads on to the second part of the problem. How to count the number of items once the data has been loaded.

Counting the number of elements in a Telerik Combobox / DropDownList or AutoComplete UI component

 

Here is the complete code including the cascading:

 

All comments welcome…..

 

  • Matthieu

    Thanks for the information, nicely described, clean code and helpfull for a fellow Telerik user.