In this article we will introduce with QueryExtender control in ASP.NET using C#.
QueryExtender Example in ASP.NET using C#
QueryExtender control in ASP.NET is used to create filters for data.That is
retrieved data from datasource without using any query. The queryExtender
control takes a collection of DataSourceExpression. Which are following:
1. OrderByExpression
2. MethodExpression
3. PropertyExpression
4. RangeExpression
5. SearchExpression
6. CustomExpression.
In this example we use a GridView, LinqDataSource and a Query extender. You will
drag and drop QueryExtender in web page.
QueryExtender.aspx (Design Page):
QueryExtender.aspx (source code):
<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" AutoEventWireup="true" CodeFile="QueryExtender.aspx.cs" Inherits="QueryExtender" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> <style type="text/css"> .style2 { font-family: Verdana; font-size: small; } </style> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> <div> <h2 style="color: Green"> QueryExtender in ASP.NET 4, C#</h2> <strong>Search By Product Name</strong><span class="style2"><strong>:</strong></span> <asp:TextBox ID="productname_Txt" runat="server"></asp:TextBox> <asp:Button ID="Search_Button" runat="server" Text="Search Products" Width="108px" /> <br /> <br /> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ProductID" DataSourceID="LinqDataSource1" ForeColor="#333333" GridLines="None" PageSize="8" Width="405px"> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowSelectButton="True" /> <asp:BoundField DataField="ProductID" HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" /> <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" /> <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" /> </Columns> <EditRowStyle BackColor="#7C6F57" /> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#E3EAEB" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#F8FAFA" /> <SortedAscendingHeaderStyle BackColor="#246B61" /> <SortedDescendingCellStyle BackColor="#D4DFE1" /> <SortedDescendingHeaderStyle BackColor="#15524A" /> </asp:GridView> <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="LinqToSqlClassDataContext" EnableDelete="True" EnableInsert="True" EnableUpdate="True" EntityTypeName="" TableName="Products"> </asp:LinqDataSource> <asp:QueryExtender ID="QueryExtender1" runat="server" TargetControlID="LinqDataSource1"> <asp:SearchExpression DataFields="ProductName" SearchType="StartsWith"> <asp:ControlParameter ControlID="productname_Txt" /> </asp:SearchExpression> </asp:QueryExtender> </div> </asp:Content>
Output:
When you run the application it will look like this.
Now write product name in textbox and click search products. You can see the following image.
Download source code
[ 0 ] Comments