Databases Reference
In-Depth Information
LISTING 5.5
Simple Search Page with DynamicFilter Controls (Markup)
<%@ Page Title=”” Language=”C#”
MasterPageFile=”~/Site.master” CodeBehind=”SamplePage.aspx.cs”
Inherits=”WebApplication.Samples.Ch05.DynamicFilter.SamplePage” %>
<asp:Content ContentPlaceHolderID=”main” runat=”server”>
Customer City:
<asp:DynamicFilter runat=”server” ID=”cityFilter”
DataField=”City” FilterUIHint=”Text” />
Title:
<asp:DynamicFilter runat=”server” ID=”titleFilter”
DataField=”ContactTitle” FilterUIHint=”Text” />
<asp:Button runat=”server” Text=”Find” />
<asp:GridView ID=”gridView” runat=”server” DataSourceID=”dataSource” />
<asp:EntityDataSource runat=”server” ID=”dataSource”
ConnectionString=”name=NorthwindEntities”
DefaultContainerName=”NorthwindEntities” EntitySetName=”Customers” />
<asp:QueryExtender runat=”server” TargetControlID=”dataSource”>
<asp:DynamicFilterExpression ControlID=”cityFilter” />
<asp:DynamicFilterExpression ControlID=”titleFilter” />
</asp:QueryExtender>
</asp:Content>
At run time, the new version of the page looks identical to the original shown in Figure
5.1. However, notice that the
DynamicFilter
controls are used instead of text boxes.
DynamicFilter
is a new ASP.NET control provided by Dynamic Data. At run time, it
dynamically loads an appropriate filter template for the column specified in its
DataField
property. On this page, you have two instances of the
DynamicFilter
control, one for the
City column and one for the ContactTitle.
Each
DynamicFilter
control must be ultimately connected to a LINQ-based data source
control that queries the database. In this example, Entity Framework is used, and there is
an
EntityDataSource
control on this page, configured to retrieve rows from the
Customers table.
EntityDataSource
class inherits the
QueryCreated
event from its base
class,
QueryableDataSource
. The control fires this event
after
it has created the initial
LINQ query, an
IQueryable
instance, but
before
the query is converted to SQL and sent to
the database server for execution. During this event, the query can be modified by the
DynamicFilter
controls.
In addition to the data source control, there's also a
QueryExtender
control on the page.
This control is associated with a
QueryableDataSource
by specifying its ID in the
TargetControlID
property of the query extender. At run time, the query extender handles
the
QueryCreated
of the data source control and passes the
IQueryable
instance to every
one of its data source expressions. This page uses two
DynamicFilterExpression
instances
to associate both of the
DynamicFilter
controls with the
QueryExtender
. Each