Dynamic List Population (Filtering Large Data Sources)
- 3 minutes to read
The ASPxComboBox provides you with the ability to manually populate a dropdown list with the required item portions, based on the currently applied filter criteria and the performed list scrolling. For instance, when using a large data source, you can only request a few records to be visible on screen. The rest of the items can be loaded on demand, while an end-user is scrolling the list.
In this mode, all data-aware operations (such as filtering) are intended to be performed on the database server side. This means that you can significantly decrease web server workload by loading only a subset of all items when working with large datasets. All of this guarantees rapid data processing and display.
To manually supply list data to the ASPxComboBox editor, you should enable the callback mode (by setting the ASPxAutoCompleteBoxBase.EnableCallbackMode property to True) and handle the following two events: ASPxComboBox.ItemsRequestedByFilterCondition and ASPxComboBox.ItemRequestedByValue.
- The ASPxComboBox.ItemsRequestedByFilterCondition event is generated on-demand on the server side when the editor requests new items for display. For example, when a filter criteria is being changed by an end-user or any scroll operation is being performed within the editor’s dropdown list. This event provides you with information about the current filter condition (ListEditItemsRequestedByFilterConditionEventArgs.Filter) and the item collection range to be displayed within the dropdown list (ListEditItemsRequestedByFilterConditionEventArgs.BeginIndex, ListEditItemsRequestedByFilterConditionEventArgs.EndIndex).
- The ASPxComboBox.ItemRequestedByValue event is automatically generated each time a ASPxComboBox editor needs to identify the editor’s selected item (ASPxComboBox.SelectedItem) by obtaining the corresponding data item specified by its value (ListEditItemRequestedByValueEventArgs.Value).
Handling these events, together with using the ‘Contains’ filter mode of the ASPxComboBox editor, allows you to deliver your customers the capability for fast and efficient searches against a large amount of data.
Online Demo
Example
The following section of the Filtering Large Data Source online demo illustrates how the ASPxComboBox.ItemsRequestedByFilterCondition and ASPxComboBox.ItemRequestedByValue events of the ASPxComboBox editor can be handled to filter the editor’s data source containing a large number of records.
Note
To work properly, both the ItemsRequestedByFilterCondition and ItemRequestedByValue events should be handled.
protected void ASPxComboBox_OnItemsRequestedByFilterCondition_SQL(object source,
ListEditItemsRequestedByFilterConditionEventArgs e) {
ASPxComboBox comboBox = (ASPxComboBox)source;
SqlDataSource1.SelectCommand = @"SELECT OID, [From], Sent, Subject
FROM (select OID, [From], Sent, Subject, row_number() over(order by t.Sent) as [rn]
from dbo.ServerSideGridTest as t WHERE ((t.Subject LIKE @filter) OR (t.[From]
LIKE @filter))) as st where st.[rn] between @startIndex and @endIndex";
SqlDataSource1.SelectParameters.Clear();
SqlDataSource1.SelectParameters.Add("filter", TypeCode.String, string.Format("%{0}%", e.Filter));
SqlDataSource1.SelectParameters.Add("startIndex", TypeCode.Int64, (e.BeginIndex + 1).ToString());
SqlDataSource1.SelectParameters.Add("endIndex", TypeCode.Int64, (e.EndIndex + 1).ToString());
comboBox.DataSource = SqlDataSource1;
comboBox.DataBind();
}
protected void ASPxComboBox_OnItemRequestedByValue_SQL(object source,
ListEditItemRequestedByValueEventArgs e) {
ASPxComboBox comboBox = (ASPxComboBox)source;
SqlDataSource1.SelectCommand = @"SELECT OID, Subject, [From], Sent
FROM dbo.ServerSideGridTest WHERE (OID = @OID) ORDER BY Sent";
SqlDataSource1.SelectParameters.Add("OID", TypeCode.Int64, e.Value.ToString());
...
comboBox.DataSource = SqlDataSource1;
comboBox.DataBind();
}