ASP.Net QueryExtender Control and DomainDataSource

12 באפריל 2009

ASP.Net QueryExtender Control and DomainDataSource

ASP.Net QueryExtender DomainDataSourceJust another post about .Net RIA Services with Silverlight and ASP.Net. Previously, I wrote about Using DomainDataSource in ASP.Net and ASP.Net DomainDataSource with Select Parameters. In this post I am talking about the new ASP.Net QueryExtender Control, that applies additional filtering to the DomainDataSource. It does that by an additional expression to the expression tree that the DomainDataSource generates before hitting the DomainService with the query.

You can download the full source code for this sample: QueryExtender Sample.zip

To use the ASP.Net QueryExtender control, you first need to add a reference to Microsoft.Web.Extensions.dll (can be found at: c:\Program Files\Microsoft SDKs\RIA Services\v1.0\Libraries\Server\ folder).

Then, register the its namespace with a tag prefix at the top of the page:

<%@ Register TagPrefix="asp"

  Namespace="Microsoft.Web.Data.UI.WebControls"

  Assembly="Microsoft.Web.Extensions" %>

And now we can add the QueryExtender control to target the customersDataSource.

<asp:DomainDataSource ID="customersDataSource" …>

</asp:DomainDataSource>

<asp:QueryExtender runat="server" TargetControlID="customersDataSource">

</asp:QueryExtender>

The QueryExtender control takes a collection of DataSourceExpressions. There are several of those in the same assenbly:

  • OrderByExpression
  • MethodExpression
  • PropertyExpression
  • RangeExpression
  • SearchExpression
  • and also CustomExpression.

To use any of those expressions, we’ll have to register another tag prefix with their namespace:

<%@ Register TagPrefix="asp"

  Namespace="Microsoft.Web.Data.UI.WebControls.Expressions"

  Assembly="Microsoft.Web.Extensions" %>

Lets filter a list by customers name. We’ll add a TextBox that will contain the search term:

<asp:TextBox runat="server" ID="txtSearch" />

and then add the expression, and bind it to a the control’s value.

<asp:QueryExtender runat="server" TargetControlID="customersDataSource">

  <asp:SearchExpression SearchType="Contains" DataFields="Name">

    <asp:ControlParameter ControlID="txtSearch" />

  </asp:SearchExpression>

</asp:QueryExtender>

If we now run the application, we can filter by the customer name.

ASP.Net QueryExtender DomainDataSource

Enjoy!

Add comment
facebook linkedin twitter email