Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to implement search in a GridView in ASP.NET

Author: Nikki Smith
Download Source Code : 1425_SearchInGridView.zip

In this article, I will show you how you can implement search in a GridView in ASP.NET.

 

 
GridView is a famous, versatile and rich control that is extensively used to display and edit data in ASP.NET web applications. The more you explore and use it, the more you realize its power and benefits. It doesn’t have built-in functionality of searching records but you can implement this functionality by yourself. In this article, I will explain one of the techniques to implement search functionality in GridView.
 
  1. Create a new Empty Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add a Web Form in the Web Site. No Need to change name of the Page
  3. Add below controls in Web Form including a GridView at the end

    <asp:Label ID="lblSearch" runat="server" Text="I want to Search"></asp:Label>
    <asp:DropDownList ID="ddlSearch" runat="server" AutoPostBack="true">
    <asp:ListItem>ContactName</asp:ListItem>
    <asp:ListItem>CompanyName</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:Label ID="lblName" runat="server" Text="Enter Contact Name"></asp:Label>
    <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="Search" />
    <asp:Label ID="lblMessage" runat="server"></asp:Label>
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
     
  4. Namespaces used in the code

    Visual Basic
     
    Imports System.Data
    Imports System.Data.SqlClient
     
    Visual C#
     
    using System.Data;
    using System.Data.SqlClient;
     
  5. Write below functions in code behind file to get data from Customers table of NORTHWND database

    In both these functions, I have get data from Customers table of NORTHWND database. The only difference is in the SELECT query. In CustomersData() function, I have get all the data in the table without any clause. In CustomerDataWithSearchText() function, I have get data from Customers table on the basis of search text and the field name is also taken as parameter of the function.
     
    Visual Basic
     
    Private Function CustomersData() As DataSet
           Dim text As String = "SELECT * FROM Customers"
           Dim connString As String = "Data Source=Local;Initial Catalog=NORTHWND;Integrated Security=True"
           Dim conn As New SqlConnection(connString)
           Dim cmd As New SqlCommand(text, conn)
           conn.Open()
           Dim da As New SqlDataAdapter(cmd)
           Dim ds As New DataSet()
           da.Fill(ds)
           conn.Close()
           Return ds
    End Function

    Private Function CustomersDataWithSearchText(ByVal field As String, ByVal searchText As String) As DataSet
           Dim text As String = "SELECT * FROM Customers WHERE " & field & " LIKE '%" & searchText & "%'"
           Dim connString As String = "Data Source=Local;Initial Catalog=NORTHWND;Integrated Security=True"
           Dim conn As New SqlConnection(connString)
           Dim cmd As New SqlCommand(text, conn)
           conn.Open()
           Dim da As New SqlDataAdapter(cmd)
           Dim ds As New DataSet()
           da.Fill(ds)
           conn.Close()
           Return ds
    End Function
     
    Visual C#
     
    private DataSet CustomersData()
    {
           string text = "SELECT * FROM Customers";
           string connString = "Data Source=Local;Initial Catalog=NORTHWND;Integrated Security=True";
           SqlConnection conn = new SqlConnection(connString);
           SqlCommand cmd = new SqlCommand(text, conn);
           conn.Open();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds);
           conn.Close();
           return ds;
    }
     
    private DataSet CustomersDataWithSearchText(string field, string searchText)
    {
           string text = "SELECT * FROM Customers WHERE " + field + " LIKE '%" + searchText + "%'";
           string connString = "Data Source=Local;Initial Catalog=NORTHWND;Integrated Security=True";
           SqlConnection conn = new SqlConnection(connString);
           SqlCommand cmd = new SqlCommand(text, conn);
           conn.Open();
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataSet ds = new DataSet();
           da.Fill(ds);
           conn.Close();
           return ds;
    }
     
  6. Write code below in Page Load event

    In Page Load event, I have bound CustomerData() function with GridView control when the page is not posted back. When the page is posted back, I have set the label text according to the selected item in DropDownList.
     
    Visual Basic
     
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
           If Not IsPostBack Then
                  GridView1.DataSource = CustomersData()
                  GridView1.DataBind()
           Else
                  If ddlSearch.SelectedItem.Text = "ContactName" Then
                         lblName.Text = "Enter ContactName:"
                         lblMessage.Text = ""
                  Else
                         lblName.Text = "Enter CompanyName:"
                         lblMessage.Text = ""
                  End If
           End If
    End Sub
     
    Visual C#
     
    protected void Page_Load(object sender, System.EventArgs e)
    {
           if (!IsPostBack)
           {
                  GridView1.DataSource = CustomersData();
                  GridView1.DataBind();
           }
           else
           {
                  if (ddlSearch.SelectedItem.Text == "ContactName")
                  {
                         lblName.Text = "Enter ContactName:";
                         lblMessage.Text = "";
                  }
                  else
                  {
                         lblName.Text = "Enter CompanyName:";
                         lblMessage.Text = "";
                  }
           }
    }
     
  7. Write code below in Button Click event

    In Button click event, I have called the CustomerDataWithSearchText() function for any searched text. The method will pass selected field to search text and the text to search as parameters.
     
    Visual Basic
     
    Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click
           If txtSearch.Text = "" Then
                  lblMessage.Text = "Enter " & ddlSearch.SelectedItem.Text & " to search"
                  lblMessage.ForeColor = Drawing.Color.Red
                  GridView1.DataSource = CustomersData()
                  GridView1.DataBind()
           Else
                  lblMessage.Text = ""
                  GridView1.DataSource = CustomersDataWithSearchText(ddlSearch.SelectedItem.Text, txtSearch.Text)
                  GridView1.DataBind()
           End If
    End Sub
     
    Visual C#
     
    protected void btnSearch_Click(object sender, System.EventArgs e)
    {
           if (string.IsNullOrEmpty(txtSearch.Text))
           {
                  lblMessage.Text = "Enter " + ddlSearch.SelectedItem.Text + " to search";
                  lblMessage.ForeColor = System.Drawing.Color.Red;
                  GridView1.DataSource = CustomersData();
                  GridView1.DataBind();
           }
           else
           {
                  lblMessage.Text = "";
                  GridView1.DataSource = CustomersDataWithSearchText(ddlSearch.SelectedItem.Text, txtSearch.Text);
                  GridView1.DataBind();
           }
    }
  8. Now you can see website in your browser.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to call user control events and functions in ASPX page

Disclaimer - Privacy
© 2002-2017 DevASP.net