Query Extender in ASP.NET

Introduction

This article demonstrates an interesting and very useful concept in ASP.NET.

Question: What is query extender?

In simple terms "It filters the data from a selected data source".

Step 1: Create a new web application

Output1.jpg

Step 2: Adding a new LINQ-to-SQL

Output2.jpg

Step 3: Add new data source to GridView



Output3.png



Output5.jpg


Output6.png


Output7.png


Output8.png



Step 4: The complete code of WebForm1.aspx is as in the following:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="QueryExtenderApp.WebForm1" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <center>

        <div>

            <table>

                <tr>

                    <td colspan="3" align="center">

                        <asp:Label ID="Label2" runat="server" Text="Query Extender" Font-Bold="true" Font-Names="Verdana"

                            Font-Size="Large"></asp:Label>

                    </td>

                </tr>

                <tr>

                    <td>

                        <asp:Label ID="Label1" runat="server" Font-Names="Verdana" Font-Bold="true" Text="Please Filter By FirstName"></asp:Label>

                    </td>

                    <td>

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

                    </td>

                </tr>

            </table>

        </div>

        <div>

            <asp:GridView ID="GridView1" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan"

                BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" AutoGenerateColumns="False"

                DataKeyNames="EmpId" DataSourceID="LinqDataSource1">

                <AlternatingRowStyle BackColor="PaleGoldenrod" />

                <Columns>

                    <asp:BoundField DataField="EmpId" HeaderText="EmpId" InsertVisible="False" ReadOnly="True"

                        SortExpression="EmpId" />

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

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

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

                </Columns>

                <FooterStyle BackColor="Tan" />

                <HeaderStyle BackColor="Tan" Font-Bold="True" />

                <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />

                <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />

                <SortedAscendingCellStyle BackColor="#FAFAE7" />

                <SortedAscendingHeaderStyle BackColor="#DAC09E" />

                <SortedDescendingCellStyle BackColor="#E1DB9C" />

                <SortedDescendingHeaderStyle BackColor="#C2A47B" />

            </asp:GridView>

            <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="QueryExtenderApp.DataClasses1DataContext"

                EntityTypeName="" TableName="Employees">

            </asp:LinqDataSource>

            <br />

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

                <asp:SearchExpression DataFields="FirstName" SearchType="StartsWith">

                    <asp:ControlParameter ControlID="TextBox1" />

                </asp:SearchExpression>

            </asp:QueryExtender>

        </div>

    </center>

    </form>

</body>

</html>

Step 5: The complete code of WebForm1.aspx.cs is as in the following:
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

namespace QueryExtenderApp

{

    public partial class WebForm1 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

        }

    }

}

Step 6: The output of the application is as in the following:

Output9.png



Step 7: The selected output of the application is as in the following:

Output10.png





I hope this article was useful for you.

Up Next
    Ebook Download
    View all
    Learn
    View all
    MVC Corporation is consulting and IT services based company.