CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Building an Efficient Search Page Using QueryExtender control in ASP.Net 4.0

By Satheesh Babu
Posted On Sep 08,2009
Article Rating:
Be first to rate
this article.
No of Comments: 8
Category: ASP.Net
Print this article.

Building an Efficient Search Page Using QueryExtender control in ASP.Net 4.0

 

Building a search page is one of the very common and repeated tasks we do in any data driven websites. To do this, we will build a select query with where clause based on the search parameter supplied through the input controls on the page. With the introduction of .Netframework 2.0, the data access is made easier with the help of new set of data access controls called DataSource controls. But, building complex filtering conditions that is required to build a search page is still complex with the help of existing datasource controls. Understanding this need, Microsoft has introduced a new extender control called QueryExtender control with ASP.Net 4.0(Currently in beta 1) release that works with LINQ.

 

What is QueryExtender control?

A QueryExtender control can be used to do efficient filtering on data at the database end with the help of LINQ technology. This means this control works adjacent with the LINQDataSource and EntityDataSource control to do filteration at the back end of your application.

 

Pre-Requisites

Visual Studio 2010 and ASP.Net 4.0

The current version of visual studio 2010 and .Netframework 4.0 is beta 1. You can download the Visual Studio 2010 beta 1 and .net FX 4 from here.  To know more about downloading and installing newer version of Visual Studio, please refer here.  Download the products and install it.

Note

Since the products are still in beta, the functionality discussed here are subject to change. If you have feedback or bug, please submit at the visual studio support page.

 

To have a better understanding, we will build a sample application that searches employee information from the table Employees.

Steps

1.      Open Visual Studio 2010.

2.      Create a new Asp.Net Website.

3.      From the solution explorer, add a new SQL express database inside App_Data folder and create a table called Employees.

Refer my Server explorer,

Using QueryExtender control in ASP.Net 4.0

 

As i said earlier, the new QueryExtender control will work along with LINQDataSource control. Hence, to proceed with we need to first design the LINQ to SQL classes.

To do this, right click your project in solution explorer and click “Add New Item..”. Select “Linq to SQL Classes” and click OK. You can rename the class if you require.

 

Creating the LINQ to SQL classes

Open Server Explorer, drag the Employee table into LINQ to SQL designer and click Save to create the LINQ to SQL classes.  Refer the below figure.

Using QueryExtender control in ASP.Net

Next, we will design our search page with the help of LINQDataSource control and QueryExtender control.

 

Creating Search Page

Drag a GridView control and LINQDataSource control from the data tab of visual studio 2010. Configure the LINQDataSource control to use our DataContext class. Next, configure the GridView control’s DataSourceID property to the ID of LINQDataSource control.

Refer the code below,

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

        DataKeyNames="EmpNo" DataSourceID="LinqDataSource1">

        <Columns>

            <asp:BoundField DataField="EmpNo" HeaderText="EmpNo" InsertVisible="False"

                ReadOnly="True" SortExpression="EmpNo" />

            <asp:BoundField DataField="EmpName" HeaderText="EmpName"

                SortExpression="EmpName" />

                 <asp:BoundField DataField="Age" HeaderText="Age"

                SortExpression="Age" />

            <asp:BoundField DataField="Address" HeaderText="Address"

                SortExpression="Address" />

            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

            <asp:BoundField DataField="Country" HeaderText="Country"

                SortExpression="Country" />

            <asp:CheckBoxField DataField="IsTemporaryEmployee"

                HeaderText="IsTemporaryEmployee" SortExpression="IsTemporaryEmployee" />

        </Columns>

    </asp:GridView>

    <asp:LinqDataSource ID="LinqDataSource1" runat="server"

        ContextTypeName="DataClassesDataContext" EntityTypeName=""

        TableName="Employees">

    </asp:LinqDataSource>

 

Execute the page. You can see the employee information populated on the screen. Next, we will add some search functionality in the page and use QueryExtender control to configure search.

 

To have a simple understanding, we build our search page that searches based on the employee name typed in a textbox control. To do this, we will drag a TextBox control and Button control to the page. 

 

Now, we will add a QueryExtender control that uses our LINQDataSource control to provide search based on employee name. The QueryExtender control will perform the filtration based on the filtration parameter configured with the QueryExtender control. In our case, it is SearchExpression that needs to be configured to get the parameter value from textbox control for searching.

 

Refer the code below,

Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />

<asp:Button ID="Button1" runat="server" Text="Search" />

 

//GridView code, Refer the code in previous section

 

 <asp:LinqDataSource ID="LinqDataSource1" runat="server"

        ContextTypeName="DataClassesDataContext" EntityTypeName=""

        TableName="Employees">

    </asp:LinqDataSource>

     <asp:QueryExtender ID="QueryExtender1"  TargetControlID="LinqDataSource1" runat="server">             

      <asp:SearchExpression DataFields="EmpName"

        SearchType="StartsWith">

        <asp:ControlParameter ControlID="txtName" />   

      </asp:SearchExpression>   

    </asp:QueryExtender>

 

When you execute the page, you may get the following error “Unknown server tag 'asp:SearchExpression'” with visual Studio 2010 beta 1.

 

To fix this error, add the following tag prefix and namespace to the controls section of Pages tag in web.config file,

<pages>

<controls>

        <add tagPrefix="asp" namespace="System.Web.UI.WebControls.Expressions" assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />

       ....

</controls>

</pages>

 

Now, execute the page and you can see the search in section.

Refer the below figure,

Using QueryExtender control in ASP.Net 4.0

In coming sections, we will try to implement more searching options the QueryExtender offers us.

 




Search based on Range of Values

Sometimes, it is required to search based on range of values. For example, to search employees based on the age that falls in a range (From till To).

To do this, we can use RangeExpression packed with QueryExtender control.

Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />

        Age From: <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox><br />

        Age To: <asp:TextBox ID="txtTo" runat="server"></asp:TextBox><br />

        <asp:Button ID="Button1" runat="server" Text="Search" />

 

//GridView code, Refer the code in previous section

 

    <asp:LinqDataSource ID="LinqDataSource1" runat="server"

        ContextTypeName="DataClassesDataContext" EntityTypeName=""

        TableName="Employees">

    </asp:LinqDataSource>

     <asp:QueryExtender ID="QueryExtender1"  TargetControlID="LinqDataSource1" runat="server">             

      <asp:SearchExpression DataFields="EmpName"

        SearchType="StartsWith">

               <asp:ControlParameter ControlID="txtName" />    

      </asp:SearchExpression> 

     <asp:RangeExpression DataField="Age" MinType="Inclusive" 

    MaxType="Inclusive">

        <asp:ControlParameter ControlID="txtFrom" />

        <asp:ControlParameter ControlID="txtTo" />

    </asp:RangeExpression>  

    </asp:QueryExtender>

 

Execute the page and see it in action. Refer the below figure,

Using QueryExtender control in ASP.Net 4.0

 

Search based on a Value

We can use PropertyExpression if we want to search based on a value. For example, if we want to search employees based on value on a column like age, name etc. To list employee who are permanent and age equal to a value,

 

Name: <asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />

        Age From: <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox><br />

        Age To: <asp:TextBox ID="txtTo" runat="server"></asp:TextBox><br />

        Temporary Employee: <asp:CheckBox ID="chkIsTempEmployee" runat="server" /><br />

         Age <asp:TextBox ID="txtAge" runat="server"></asp:TextBox><br />

        <asp:Button ID="Button1" runat="server" Text="Search" />

   

    <asp:LinqDataSource ID="LinqDataSource1" runat="server"

        ContextTypeName="DataClassesDataContext" EntityTypeName=""

        TableName="Employees">

    </asp:LinqDataSource>

     <asp:QueryExtender ID="QueryExtender1"  TargetControlID="LinqDataSource1" runat="server">             

      <asp:SearchExpression DataFields="EmpName"

        SearchType="StartsWith">

               <asp:ControlParameter ControlID="txtName" />   

      </asp:SearchExpression> 

     <asp:RangeExpression DataField="Age" MinType="Inclusive" 

    MaxType="Inclusive">

        <asp:ControlParameter ControlID="txtFrom" />

        <asp:ControlParameter ControlID="txtTo" />

    </asp:RangeExpression>

   <asp:PropertyExpression>

    <asp:ControlParameter ControlID="chkIsTempEmployee" Name="IsTemporaryEmployee" />

  </asp:PropertyExpression>

   <asp:PropertyExpression>

    <asp:ControlParameter ControlID="txtAge" Name="Age" />

  </asp:PropertyExpression>

    </asp:QueryExtender>

 

Execute the page and see it in action.

Refer the below figure,

Using QueryExtender control in ASP.Net 4.0

 

Clearly, the new QueryExtender control has made some of the advance search implementation very easier.

I have attached source code discussed in this article in next section.

 

Downloads

Download Source 

Reference

http://msdn.microsoft.com/en-us/library/dd537669(VS.100).aspx

 

Conclusion

Thus, the new QueryExtender control released with ASP.Net 4.0(currently in beta) simplifies the building of one of most frequently done filtering task. We will see more about new features and enhancements with the latest framework in coming days. Download the source attached with this article to see it in action.

 You can download the Visual Studio 2010 beta 1 and .net FX 4 from here. If you have feedback or bug, please submit at the visual studio support page.

Happy Coding!!

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Thank You!
Thanks for this tutorial, you have given me new hope for my search page. I have been looking for a solution like this because I able to use "full-text search" in SQL Server 2008 - seemed too complex for me. . . so thanks again, much appreciated :)

- Naiomi
Thanks
Nice article !

Thanks
Mr
nice one..... good demo
Postback
I'm having issues with continuing the search after initial search. Let's say I search for John Doe, if I want to search for Jane Smith after I get results, the search does not execute? Is this a Postback issue?
How show the results in a grid view in another page
I was wondering if anyone know how show the results of the search in in a grid view in another page.

Any suggestion?

my email is: marcelocbacchi@hotmail.com

Thanks
Mr
Excellent tutorial. Easy to follow and helpful. Thank you very much indeed. :)
Mr.
Great article to read & explore the functionality of QueryExtender.
Article Feedback
Excellent Article!!