Searching With the Entity Framework Data Source

A search mechanism is something unavoidable for most applications. A common scenario is to provide the user a set of filters for which he can supply values and incrementally restrict the number of records that satisfy these restrictions.

Search Form

It’s also normal for the user not to use all the filters supported by the search form which means that the unused filters must be excluded from the search query to not affect the result. A quick solution to this problem is to generate this search query dynamically and omit the empty filters.

However in this example I will be taking advantage of the fact that SQL Server supports expression short-circuiting to use an Entity Framework data source control with a static Where clause.

<asp:EntityDataSource ID="eds" runat="server"
    ConnectionString="name=EnContext" DefaultContainerName="EnContext"
    Select="it.ID, it.Name, it.Type, it.Discontinued"
    Where="(@NameFilterEmpty OR it.Name LIKE @NameFilter) AND (@TypeFilterEmpty OR it.Type = @TypeFilter) AND (@DiscontinuedFilterEmpty OR it.Discontinued = @DiscontinuedFilter)">
    <My:ControlFormatParameter Name="NameFilter" ControlID="nameTB"
      DbType="String" Format="%{0}%" />
    <My:EmptyControlCheckParameter Name="NameFilterEmpty"
      ControlID="nameTB" DbType="Boolean" />
    <asp:ControlParameter Name="TypeFilter" ControlID="typeDD"
      DbType="Int32" PropertyName="SelectedValue" />
    <My:EmptyControlCheckParameter Name="TypeFilterEmpty"
      ControlID="typeDD" DbType="Boolean"
      PropertyName="SelectedValue" EmptyValue="-1" />
    <asp:ControlParameter Name="DiscontinuedFilter"
      ControlID="discontinuedRBL" DbType="Boolean" />
    <My:EmptyControlCheckParameter Name="DiscontinuedFilterEmpty"
      ControlID="discontinuedRBL" DbType="Boolean" />

As you see in the previous code snippet each search filter is controlled by a specific boolean parameter that when true disables the associated filter. The classes ControlFormatParameter and EmptyControlCheckParameter both derive from ASP .NET ControlParameter and allow in the first case to format the value of the associated input control before passing it to the data source and in the second case to switch each search filter on/off by evaluating if the associated input control has an empty value.

public class ControlFormatParameter : ControlParameter
    public string Format { get; set; }

    protected override object Evaluate(HttpContext context, Control control)
        return String.Format(this.Format, base.Evaluate(context, control));

public class EmptyControlCheckParameter : ControlParameter
    public EmptyControlCheckParameter()
        this.EmptyValue = string.Empty;

    public string EmptyValue { get; set; }

    protected override object Evaluate(HttpContext context, Control control)
        string value = base.Evaluate(context, control).ToString();

        if (String.Equals(value, this.EmptyValue, StringComparison.OrdinalIgnoreCase))
            return true;

        return false;

2 thoughts on “Searching With the Entity Framework Data Source”

  1. Thanks for posting!

    To use this, you need to add a reference to the project first. Or register at the page level:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s