OnDataSourceSelect
The OnDataSourceSelect event is raised just before the result items are requested from the underlying DataSource, providing the ability to modify the parameters of the request. For instance with SqlDataSource the user can modify the SelectCommand and implement the filtering directly on the SQL Server.
The event handler receives two parameters:
-
The instance of the SearchBox 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 SearchBox.
-
SelectedContextItem - returns the selected context item in the SearchContext.
-
UserContext - returns the userContext object of type IDictionary that is passed through the OnClientDataRequesting event.
-
Here is a sample code demonstrating how this event can be used:
<telerik:RadSearchBox RenderMode="Lightweight" runat="server" ID="RadSearchBox1"
OnDataSourceSelect="RadSearchBox1_DataSourceSelect"
DataSourceID="SqlDataSource1"
DataKeyNames="UnitPrice"
DataValueField="ProductID"
DataTextField="ProductName">
<DropDownSettings Height="400" Width="300" />
</telerik:RadSearchBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Products]">
</asp:SqlDataSource>
protected void RadSearchBox1_DataSourceSelect(object sender, SearchBoxDataSourceSelectEventArgs e)
{
SqlDataSource source = (SqlDataSource)e.DataSource;
RadSearchBox searchBox = (RadSearchBox)sender;
string likeCondition = string.Format("'{0}' + @filterString + '%'", searchBox.Filter == SearchBoxFilter.Contains ? "%" : "");
string countCondition = e.ShowAllResults ? " " : " TOP 100 ";
source.SelectCommand = string.Format("SELECT {0}* FROM [Products] WHERE [" + searchBox.DataTextField + "] LIKE {1}", countCondition, likeCondition);
source.SelectParameters.Add("filterString", e.FilterString.Replace("%", "[%]").Replace("_", "[_]"));
}