Here is a code example of searching a GridView control in ASP.NET.
Create an ASP.NET Web Application using Visual Studio.
Step 1: Download ziped Bin folder and copy it to your Bin folder
Step 2: Add dll to your toolbox and drag it to your web page.
Step 3: Now, change the default aspx page. I add some controls to the page. See below.
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <%@ Register assembly="SamApp.WebControls.SearchGridView" namespace="SamApp.WebControls" tagprefix="cc1" %>
- <!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>
- <p>
- <br />
- </p>
- <form id="form1" runat="server">
- <div style="height: 100px">
- <asp:HiddenField ID="hfSearchText" runat="server" />
- <asp:HiddenField ID="hfSort" runat="server" />
- </div>
- <div>
- <cc1:SearchGridView ID="SearchGridView1" runat="server" BackColor="White"
- BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
- EnableModelValidation="True"
- onpageindexchanging="SearchGridView1_PageIndexChanging"
- onsearchgrid="SearchGridView1_SearchGrid" ShowFooter="True"
- AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
- onsorting="SearchGridView1_Sorting" ForeColor="Black" GridLines="Vertical">
- <SearchFilters>
- <asp:ListItem>Username</asp:ListItem>
- <asp:ListItem Value="User_Pass">Password</asp:ListItem>
- <asp:ListItem Value="F_Name">First Name</asp:ListItem>
- <asp:ListItem Value="L_Name">Last Name</asp:ListItem>
- <asp:ListItem Value="City">City</asp:ListItem>
- </SearchFilters>
- <AlternatingRowStyle BackColor="#CCCCCC" />
- <Columns>
- <asp:BoundField DataField="Username" HeaderText="Username"
- SortExpression="Username" />
- <asp:BoundField DataField="User_Pass" HeaderText="Password"
- SortExpression="User_Pass" />
- <asp:BoundField DataField="F_Name" HeaderText="First Name"
- SortExpression="F_Name" />
- <asp:BoundField DataField="L_Name" HeaderText="Last Name"
- SortExpression="L_Name" />
- <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
- </Columns>
- <FooterStyle BackColor="#CCCCCC" />
- <HeaderStyle BackColor="#333399" Font-Bold="True" ForeColor="Silver" />
- <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
- <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
- </cc1:SearchGridView>
- <br />
- <br />
- </div>
- <div>
- </div>
- </form>
- </body>
- </html>
Step 4: Code behind
Here is the code behind. In this code, I load data from a database table User_Login. Make sure you need to change your database connection string, SELECT statement based on your database table.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- public partial class _Default : System.Web.UI.Page
- {
- public static SqlConnection conn = new SqlConnection(@"Your connection String!!!");
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- bind();
- }
- }
- public void bind()
- {
- SearchGridView1.DataSource = GetTable();
- SearchGridView1.DataBind();
- }
- public DataView GetTable()
- {
- SqlDataSource ds = new SqlDataSource();
- ds.ConnectionString = @"server=IT-WSPC-F10\SQL;User Id=sa;Password=inveera@123;Initial Catalog=pankaj";
- ds.SelectCommand = "select * from User_Login";
- if (hfSearchText.Value != "")
- ds.SelectCommand += " where " + hfSearchText.Value;
- DataView dv = (DataView)ds.Select(new DataSourceSelectArguments());
- if (hfSort.Value != "")
- dv.Sort = hfSort.Value;
- return dv;
- }
- protected void SearchGridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
- {
- SearchGridView1.PageIndex = e.NewPageIndex;
- bind();
- }
- protected void SearchGridView1_SearchGrid(string _strSearch)
- {
- hfSearchText.Value = _strSearch;
- bind();
- }
- protected void SearchGridView1_Sorting(object sender, GridViewSortEventArgs e)
- {
- if (hfSort.Value == e.SortExpression)
- hfSort.Value = e.SortExpression + " Desc";
- else
- hfSort.Value = e.SortExpression;
- bind();
- }
- }