New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Building Filter Expressions Manually

The RadFilter control allows you to process its expression tree manually. This can be done by translating all filter expressions to a suitable query with the help of the RadFilter query providers. Then, you can modify them or apply them directly to the filtered control.

RadFilter query providers

The following table describes all RadFilter query providers:

Query providerDescription
RadFilterDynamicLinqQueryProviderProvides the built expression for all dynamic LINQ data sources
RadFilterEntitySqlQueryProviderProvides the built expression for entity SQL data source
RadFilterGridQueryProviderProvides the built filter expression of the RadGrid
RadFilterOqlQueryProviderProvides the built expression for OQL data source
RadFilterExpressionPreviewProviderProvides the HTML of the built expression preview
RadFilterListViewQueryProviderProvides the built filter expression of the RadListView
RadFilterSqlQueryProviderProvides the built expression for SQL data source

Processing the expression tree

In order to get the expression tree built by the RadFilter control, you can use the ApplyFilterExpressions event. The following example demonstrates how to get the built expression for a RadListBox bound to LinqDataSource and display it in a label:

ASPNET
<telerik:RadFilter RenderMode="Lightweight" runat="server" ID="RadFilter1" DataSourceControlID="LinqDataSourceCategories"
    OnApplyExpressions="RadFilter1_ApplyExpressions">
</telerik:RadFilter>
<br />
<asp:LinqDataSource ID="LinqDataSourceCategories" runat="server" ContextTypeName="LinqToSql.NorthwindDataContext"
    TableName="Categories">
</asp:LinqDataSource>
<telerik:RadListBox RenderMode="Lightweight" runat="server" ID="RadListBox1" DataSourceID="LinqDataSourceCategories"
    Height="200px" Width="250px">
    <EmptyMessageTemplate>
        No records to display.
    </EmptyMessageTemplate>
    <ItemTemplate>
        <em>Category name </em><span><%# Eval("CategoryName") %></span><br/>
        <em>Description </em><span><%# Eval("Description")%></span><br />
        <telerik:RadBinaryImage ID="RadBinaryImage2" runat="server" AlternateText="Category Photo"
            Style="margin-left: 10px" ToolTip="Category Photo" Width="90px" Height="62px"
            BorderColor="White" BorderWidth="2px" ResizeMode="Fit" ImageUrl='<%# string.Format("IMG/{0}.jpg", Eval("CategoryID")) %>' />
    </ItemTemplate>
</telerik:RadListBox>
<asp:Label runat="server" ID="Log" Text=""></asp:Label>

Manipulating expressions

RadFilterQueryProvider exposes delegate which is fired every time an expressionhave been evaluated. The delegate could be used for scenarios like custom data formating.

Below is demonstrated how you could replace any EqualTo and NotEqualTo respectevely with Between and NotBetween functions when filtering by date. The approach allows you to include all hours for the selected date as by default if you filter by a date only dates which time is set to 12:00 PM will be included in the result.

ASPNET
<telerik:RadFilter RenderMode="Lightweight" runat="server" ID="RadFilter1"
    ExpressionPreviewPosition="Bottom"
    OnExpressionItemCreated="RadFilter1_ExpressionItemCreated"
    OnApplyExpressions="RadFilter1_ApplyExpressions">
    <FieldEditors>
        <telerik:RadFilterDropDownEditor FieldName="ShipCountry" DataTextField="ShipCountry"
            DataValueField="ShipCountry" />
        <telerik:RadFilterDateFieldEditor FieldName="OrderDate" PickerType="DatePicker" />
    </FieldEditors>
</telerik:RadFilter>

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" AllowPaging="true" AllowSorting="true" PageSize="5"
    runat="server" EnableLinqExpressions="false" OnNeedDataSource="RadGrid1_NeedDataSource">
</telerik:RadGrid>