Combo Box for ASP.NET Web Forms - How to load items on a callback

[Run Online]

This example shows how to load combo box items on the first click of the drop-down button.

Combo box

In this example, the ASPxComboBox control initially has one (default) item.

private const string DefaultCountryName = "United Kingdom";

protected void Page_Load(object sender, EventArgs e) {
    if(!IsCallback) {
        cbCountries.Items.Add(DefaultCountryName);
        cbCountries.SelectedIndex = 0;
    }
}

When the drop-down button is clicked, the DropDown event handler sends a callback to the server if the items are not loaded yet.

function OnDropDown(s, e) {
    if(!s.countriesLoaded) {
        s.countriesLoaded = true;
        cbCountries.PerformCallback();
    }
}

The PerformCallback method invokes the server Callback event. The event handler populates the Items collection with a list of items.

protected void OnCallback(object source, CallbackEventArgsBase e) {
    List<string> counties = new List<string>(DataProvider.GetCountries());
    counties.Remove(DefaultCountryName);
    ((ASPxComboBox)source).Items.AddRange(counties);
}

Files to Review

Documentation