Search Data Very Quickly Using jQuery In Web Application

Search data from HTML Table using Jquery without hitting the database and page post.

In this article, we will learn how to search data from a table without hitting front-end and back-end (this means without a heat database or server-side code). For searching the records from an entire page, there are two ways to do it. For searching the specific records, we will send the request using the AJAX method and search the records from the database, then the result will be shown. And another method is that we will store all the records in any collection like dataset or session or any other variable and use LINQ for filtering it.

Now, all these ways will take a long time. Your application will run very slowly if you post a huge amount of data on the server-side. For solving this issue, we will search the data from jQuery in HTML table.

Here is how to search the records using jQuery.

Let’s start it with an example.

Open Visual Studio-> Create a new project which is "SearchRecords".


 
After clicking on Project

 

 

After clicking on OK button, the below dialog box will open. Here, select emptyan template and then click on OK button. 

 
 Now, our project is created successfully. Then, we need to add a new web page which you can see in the below screen-shot.
 

After clicking on OK button, our project is created. Now, I will add gridview control for binding the records.
  1. <form id="form1" runat="server">  
  2.   <div>  
  3.            <asp:GridView ID="searchGridView" runat="server" Style="width: 80%;margin-left: 100px" ForeColor="Black" BackColor="#CCCCFF"   
  4.             BorderColor="#00FFCC" BorderStyle="Solid" Font-Names="Arial">  
  5.          <AlternatingRowStyle BackColor="#FFCC99" BorderColor="#00FFCC" />  
  6.     </asp:GridView>  
  7.    </div>  
  8. </form>  

And create an input control for entering search value.

  1. <p style="text-align: right; width: 500px; margin-top: 50px; margin-left: 150px">  
  2.        <span style="font-weight: bold;">Search:</span>  
  3.        <input type="text" id="txtSearch" name="txtSearch" maxlength="50" style="height: 25px; font: 100" />   
  4.    </p>  

Now I will fetch the records with this method.

  1. public DataTable getStudentDetails()  
  2. {  
  3.   SqlCommand cmd = new SqlCommand();  
  4.   SqlConnection con = new SqlConnection(mainConnectionString);  
  5.   cmd.CommandType = CommandType.StoredProcedure;  
  6.   cmd.CommandText = "spstudentList";  
  7.   cmd = new SqlCommand(cmd.CommandText,con);  
  8.   SqlDataAdapter adapter = new SqlDataAdapter(cmd.CommandText, con);  
  9.   
  10.   DataTable employees = new DataTable();  
  11.   adapter.Fill(employees);  
  12.   return employees;  
  13. }  

After getting the total records I will bind it in our gridview which is "searchGridview".

  1. searchGridView.DataSource = getStudentDetails();  
  2. searchGridView.DataBind();  

After successfully binding the record in gridview it looks like this.

 
Now when we enter any values for searching in the above input box we will search the record without page posting and without hitting on server-side we will use jquery on keyup event for searching very fast.
  1. $('#txtSearch').keyup(function () {  
  2.          if ($('#txtSearch').val().length > 1) {  
  3.              $('#searchGridView tr').hide();  
  4.              $('#searchGridView tr:first').show();  
  5.              $('#searchGridView tr td:containsNoCase(\'' + $('#txtSearch').val() + '\')').parent().show();  
  6.          }  
  7.          else if ($('#txtSearch').val().length == 0) {  
  8.              resetSearchValue();  
  9.          } 
  10. }); 

 Cancel the Search value if user presses Backspace or ESC key.

  1. $('#txtSearch').keyup(function (event) {  
  2.             if (event.keyCode == 27) {  
  3.                 resetSearchValue();  
  4.             }  
  5.  });  
  6.   
  7. function resetSearchValue() {  
  8.         $('#txtSearch').val('');  
  9.         $('#searchGridView tr').show();  
  10.         $('.norecords').remove();  
  11.         $('#txtSearch').focus();  
  12.     }  

Below code will shown jquery to have a : Contains selector which is case insensitive, the :contains selector remains unchanged.

  1. $.expr[":"].containsNoCase = function (el, i, m) {  
  2.        var search = m[3];  
  3.        if (!search) return false;  
  4.        return eval("/" + search + "/i").test($(el).text());  
  5.    };  
Now here is the full code for the client side.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="SearchRecords.Home" %>  
  2.   
  3. <!DOCTYPE html>  
  4. <script src="jquery-3.3.1.min.js"></script>  
  5. <script language="javascript" type="text/javascript">  
  6.     $.expr[":"].containsNoCase = function (el, i, m) {  
  7.         var search = m[3];  
  8.         if (!search) return false;  
  9.         return eval("/" + search + "/i").test($(el).text());  
  10.     };  
  11.   
  12.     $(document).ready(function () {  
  13.         $('#txtSearch').keyup(function () {  
  14.             if ($('#txtSearch').val().length > 1) {  
  15.                 $('#searchGridView tr').hide();  
  16.                 $('#searchGridView tr:first').show();  
  17.                 $('#searchGridView tr td:containsNoCase(\'' + $('#txtSearch').val() + '\')').parent().show();  
  18.             }  
  19.             else if ($('#txtSearch').val().length == 0) {  
  20.                 resetSearchValue();  
  21.             }  
  22.   
  23.             if ($('#searchGridView tr:visible').length == 1) {  
  24.                 $('.norecords').remove();  
  25.                 $('#searchGridView').append('<tr class="norecords"><td colspan="6" class="Normal" style="text-align: center">No records were found</td></tr>');  
  26.             }  
  27.         });  
  28.   
  29.         $('#txtSearch').keyup(function (event) {  
  30.             if (event.keyCode == 27) {  
  31.                 resetSearchValue();  
  32.             }  
  33.         });  
  34.     });  
  35.   
  36.     function resetSearchValue() {  
  37.         $('#txtSearch').val('');  
  38.         $('#searchGridView tr').show();  
  39.         $('.norecords').remove();  
  40.         $('#txtSearch').focus();  
  41.     }  
  42.   
  43. </script>  
  44. <html>  
  45. <body>  
  46.     <p style="text-align: right; width: 500px; margin-top: 50px; margin-left: 150px">  
  47.         <span style="font-weight: bold;">Search:</span>  
  48.         <input type="text" id="txtSearch" name="txtSearch"  placeholder="    type search text" maxlength="50" style="height: 25px; font: 100" />   
  49.     </p>  
  50.     <form id="form1" runat="server">  
  51.         <div>  
  52.             <asp:GridView ID="searchGridView" runat="server" Style="width: 80%; margin-left: 100px" ForeColor="Black" BackColor="#CCCCFF" BorderColor="#00FFCC" BorderStyle="Solid" Font-Names="Arial">  
  53.                 <AlternatingRowStyle BackColor="#FFCC99" BorderColor="#00FFCC" />  
  54.             </asp:GridView>  
  55.         </div>  
  56.     </form>  
  57. </body>  
  58. </html>  
Server side code
  1. namespace SearchRecords  
  2. {  
  3.     public partial class Home : System.Web.UI.Page  
  4.     {  
  5.         public static readonly string mainConnectionString = ConfigurationManager.ConnectionStrings["con"].ToString();  
  6.   
  7.         protected void Page_Load(object sender, EventArgs e)  
  8.         {  
  9.             searchGridView.DataSource = getStudentDetails();  
  10.             searchGridView.DataBind();  
  11.         }  
  12.   
  13.         public DataTable getStudentDetails()  
  14.         {  
  15.             SqlCommand cmd = new SqlCommand();  
  16.             SqlConnection con = new SqlConnection(mainConnectionString);  
  17.             cmd.CommandType = CommandType.StoredProcedure;  
  18.             cmd.CommandText = "spstudentList";  
  19.             cmd = new SqlCommand(cmd.CommandText,con);  
  20.             SqlDataAdapter adapter = new SqlDataAdapter(cmd.CommandText, con);  
  21.   
  22.             DataTable employees = new DataTable();  
  23.             adapter.Fill(employees);  
  24.             return employees;  
  25.         }  
  26.     }  
  27. }   
Output
 
 
Conclusion

In this article, we saw that we can search records from any control without page post. It helps us in searching records very quickly. I hope it will be helpful for all web applications nad developers everywhere.