New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
OnDataSourceSelect
OnDataSourceSelect
The OnDataSourceSelect event is raised just before the result items are requested from the underlying DataSource. Provided with the ability to modify the parameters of the request via the SelectCommand,the user can implement the filtering directly on the SQL Server. Optimizing the query in such manner would significantly boost the performance.
The event handler receives two parameters:
-
The instance of the AutoCompleteBox firing the event
-
An event arguments parameter that contains the following properties:
-
DataSource -returns a reference to the DataSource control.
-
FilterString -returns the text that is typed in the AutoCompleteBox.
Here is a sample code demonstrating how this event can be used:
ASPNET
<telerik:RadAutoCompleteBox RenderMode="Lightweight" runat="server" ID="RadAutoCompleteBox1"
OnDataSourceSelect="RadAutoCompleteBox1_DataSourceSelect"
DataSourceID="SqlDataSource1"
DataKeyNames="UnitPrice"
DataValueField="ProductId"
DataTextField="ProductName">
</telerik:RadAutoCompleteBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Products]">
</asp:SqlDataSource>
C#
protected void RadAutoCompleteBox1_DataSourceSelect(object sender, AutoCompleteBoxDataSourceSelectEventArgs e)
{
SqlDataSource source = (SqlDataSource)e.DataSource;
RadAutoCompleteBox autoCompleteBox = (RadAutoCompleteBox)sender;
string likeCondition = string.Format("'{0}' + @filterString + '%'", autoCompleteBox.Filter == RadAutoCompleteFilter.Contains ? "%" : "");
string countCondition = " TOP 10 ";
source.SelectCommand = string.Format("SELECT {0}* FROM [Products] WHERE [" + autoCompleteBox.DataTextField + "] LIKE {1}", countCondition, likeCondition);
source.SelectParameters.Add("filterString", e.FilterString.Replace("%", "[%]").Replace("_", "[_]"));
}