The following is my Data Table structure from which I am fetching data:

table design
Image 1.

Data in my table:

table
Image 2.

To do this I created the following Stored Procedure:

store procedure
Image 3.

My Stored Procedure is:

  1. CREATEPROCEDURE [dbo].[GetStudentDataWithSearch]
  2. ( @SearchTerm VARCHAR(100)='',
  3. @PageIndex INT= 1,
  4. @PageSize INT= 10,
  5. @RecordCount INTOUTPUT
  6. )
  7. AS
  8. BEGIN
  9. SETNOCOUNTON;
  10. SELECTROW_NUMBER()OVER
  11. (
  12. ORDERBY StudentID ASC
  13. )AS RowNumber
  14. ,StudentID
  15. ,Name
  16. ,Email
  17. ,Class,EnrollYear,City INTO #Results FROM Student
  18. WHERE [Name] LIKE'%'+ @SearchTerm +'%'OR @SearchTerm =''
  19. SELECT*FROM #Results
  20. WHERE RowNumber BETWEEN(@PageIndex -1)* @PageSize + 1 AND(((@PageIndex -1)* @PageSize + 1)+ @PageSize)- 1
  21. SELECT @RecordCount =COUNT(*)FROM #Results
  22. DROPTABLE #Results
  23. END
The following is my aspx code:
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="ShowSearchPaging.aspx.cs"Inherits="ShowSearchAndPagingUsing_jQuery.ShowSearchPaging"%>
  2. <!DOCTYPEhtml>
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <headrunat="server">
  5. <title>jQuery: Show Data and Paging in ASP.NET Grid View using jQuery</title>
  6. <scripttype="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
  7. <scriptsrc="jquery.pagination.min.js"type="text/javascript"></script>
  8. <scripttype="text/javascript">
  9. $(function () {
  10. GetStudents(1);
  11. });
  12. $("[id*=txtSearch]").live("keyup", function () {
  13. GetStudents(parseInt(1));
  14. });
  15. $(".Pagination .page").live("click", function () {
  16. GetStudents(parseInt($(this).attr('page')));
  17. });
  18. function SearchTerm() {
  19. return jQuery.trim($("[id*=txtSearch]").val());
  20. };
  21. function GetStudents(pageIndex) {
  22. $.ajax({
  23. type: "POST",
  24. url: "ShowSearchPaging.aspx/GetStudents",
  25. data: '{searchTerm: "' + SearchTerm() + '", pageIndex: ' + pageIndex + '}',
  26. contentType: "application/json; charset=utf-8",
  27. dataType: "json",
  28. success: OnSuccess,
  29. failure: function (response) {
  30. alert(response.d);
  31. },
  32. error: function (response) {
  33. alert(response.d);
  34. }
  35. });
  36. }
  37. var row;
  38. function OnSuccess(response) {
  39. var xmlDoc = $.parseXML(response.d);
  40. var xml = $(xmlDoc);
  41. var customers = xml.find("Student");
  42. if (row == null) {
  43. row = $("[id*=GridViewStudent] tr:last-child").clone(true);
  44. }
  45. $("[id*=GridViewStudent] tr").not($("[id*=GridViewStudent] tr:first-child")).remove();
  46. if (customers.length > 0) {
  47. $.each(customers, function () {
  48. var customer = $(this);
  49. $("td", row).eq(0).html($(this).find("Name").text());
  50. $("td", row).eq(1).html($(this).find("Email").text());
  51. $("td", row).eq(2).html($(this).find("Class").text());
  52. $("td", row).eq(3).html($(this).find("EnrollYear").text());
  53. $("td", row).eq(4).html($(this).find("City").text());
  54. $("td", row).eq(5).html($(this).find("Country").text());
  55. $("[id*=GridViewStudent]").append(row);
  56. row = $("[id*=GridViewStudent] tr:last-child").clone(true);
  57. });
  58. var pager = xml.find("dtForPaging");
  59. $(".Pagination").jQ_Pager({
  60. ActiveCssClass: "current",
  61. PagerCssClass: "pager",
  62. PageIndex: parseInt(pager.find("PageIndex").text()),
  63. PageSize: parseInt(pager.find("PageSize").text()),
  64. RecordCount: parseInt(pager.find("RecordCount").text())
  65. });
  66. $(".Name").each(function () {
  67. var searchPattern = new RegExp('(' + SearchTerm() + ')', 'ig');
  68. $(this).html($(this).text().replace(searchPattern, "<span style='background-color:red;color:white;'>" + SearchTerm() + "</span>"));
  69. });
  70. } else {
  71. var empty_row = row.clone(true);
  72. $("td:first-child", empty_row).attr("colspan", $("td", row).length);
  73. $("td:first-child", empty_row).attr("align", "center");
  74. $("td:first-child", empty_row).html("No records found for the search criteria.");
  75. $("td", empty_row).not($("td:first-child", empty_row)).remove();
  76. $("[id*=GridViewStudent]").append(empty_row);
  77. }
  78. };
  79. </script>
  80. </head>
  81. <body>
  82. <formid="form1"runat="server">
  83. <div>
  84. <tablestyle="border: solid15pxblue; width: 100%; vertical-align: central;">
  85. <tr>
  86. <tdstyle="padding-left: 20px; padding-top: 20px; padding-bottom: 20px; background-color: skyblue; text-align: center; font-family: Verdana; font-size: 20pt; color: red;">jQuery: Show Data and Paging in ASP.NET Grid View using jQuery</td>
  87. </tr>
  88. <tr>
  89. <tdstyle="padding-left: 100px; padding-top: 20px; padding-bottom: 20px; background-color: #EE9A4D; font-family: Arial; font-size: 15pt; color: #E41B17">Enter Name To Search #:
  90. <asp:TextBoxID="txtSearch"runat="server"/>
  91. </td>
  92. </tr>
  93. <tr>
  94. <td>
  95. <tablestyle="width: 80%; text-align: center; vertical-align: central;">
  96. <tr>
  97. <tdstyle="text-align: left;">
  98. <asp:GridViewID="GridViewStudent"runat="server"AutoGenerateColumns="False"Width="100%"
  99. BackColor="White"BorderColor="#3366CC"BorderStyle="None"BorderWidth="1px"CellPadding="4"GridLines="Both">
  100. <Columns>
  101. <asp:BoundFieldDataField="Name"HeaderText="Student Name"HeaderStyle-HorizontalAlign="Left"ItemStyle-CssClass="Name"></asp:BoundField>
  102. <asp:BoundFieldDataField="Email"HeaderText="Email"HeaderStyle-HorizontalAlign="Left"/>
  103. <asp:BoundFieldDataField="Class"HeaderText="Class"HeaderStyle-HorizontalAlign="Left"/>
  104. <asp:BoundFieldDataField="EnrollYear"HeaderText="Enroll Year"HeaderStyle-HorizontalAlign="Left"/>
  105. <asp:BoundFieldDataField="City"HeaderText="City"HeaderStyle-HorizontalAlign="Left"/>
  106. <asp:BoundFieldDataField="Country"HeaderText="Country"HeaderStyle-HorizontalAlign="Left"/>
  107. </Columns>
  108. <FooterStyleBackColor="#99CCCC"ForeColor="#003399"/>
  109. <HeaderStyleBackColor="#003399"Font-Bold="True"ForeColor="#CCCCFF"/>
  110. <PagerStyleBackColor="#99CCCC"ForeColor="#003399"HorizontalAlign="Left"/>
  111. <RowStyleBackColor="White"ForeColor="#003399"/>
  112. <SelectedRowStyleBackColor="#009999"Font-Bold="True"ForeColor="#CCFF99"/>
  113. <SortedAscendingCellStyleBackColor="#EDF6F6"/>
  114. <SortedAscendingHeaderStyleBackColor="#0D4AC4"/>
  115. <SortedDescendingCellStyleBackColor="#D6DFDF"/>
  116. <SortedDescendingHeaderStyleBackColor="#002876"/>
  117. </asp:GridView>
  118. </td>
  119. </tr>
  120. <tr>
  121. <td>
  122. <divclass="Pagination"style="background-color: orange; font-family: Verdana; font-size: 10pt; height: 30px; text-align: center; vertical-align: central; padding-top: 20px; padding-bottom: 10px;">
  123. </div>
  124. </td>
  125. </tr>
  126. </table>
  127. </td>
  128. </tr>
  129. </table>
  130. </div>
  131. </form>
  132. </body>
  133. </html>
Now my aspx.cs code is:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Linq;
  7. using System.Web;
  8. using System.Web.Services;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. namespace ShowSearchAndPagingUsing_jQuery
  12. {
  13. publicpartialclassShowSearchPaging : System.Web.UI.Page
  14. {
  15. privatestaticint PageSize = 5;
  16. protectedvoid Page_Load(object sender, EventArgs e)
  17. {
  18. if (!IsPostBack)
  19. {
  20. BindGridViewHeader();
  21. }
  22. }
  23. privatevoid BindGridViewHeader()
  24. {
  25. DataTable dtHeader = newDataTable();
  26. dtHeader.Columns.Add("Name");
  27. dtHeader.Columns.Add("Email");
  28. dtHeader.Columns.Add("Class");
  29. dtHeader.Columns.Add("EnrollYear");
  30. dtHeader.Columns.Add("City");
  31. dtHeader.Columns.Add("Country");
  32. dtHeader.Rows.Add();
  33. GridViewStudent.DataSource = dtHeader;
  34. GridViewStudent.DataBind();
  35. }
  36. [WebMethod]
  37. publicstaticstring GetStudents(string searchTerm, int pageIndex)
  38. {
  39. string query = "[GetStudentDataWithSearch]";
  40. SqlCommand cmd = newSqlCommand(query);
  41. cmd.CommandType = CommandType.StoredProcedure;
  42. cmd.Parameters.AddWithValue("@SearchTerm", searchTerm);
  43. cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
  44. cmd.Parameters.AddWithValue("@PageSize", PageSize);
  45. cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
  46. return GetData(cmd, pageIndex).GetXml();
  47. }
  48. privatestaticDataSet GetData(SqlCommand cmd, int pageIndex)
  49. {
  50. SqlDataAdapter da;
  51. DataSet ds = newDataSet();
  52. SqlConnection con = newSqlConnection();
  53. ds = newDataSet();
  54. con.ConnectionString = @"Data Source=MYPC\SqlServer2k8; Initial Catalog=SchoolManagement; Integrated Security=true;";
  55. cmd.Connection = con;
  56. da = newSqlDataAdapter(cmd);
  57. da.Fill(ds, "Student");
  58. con.Open();
  59. cmd.ExecuteNonQuery();
  60. con.Close();
  61. DataTable dt = newDataTable("dtForPaging");
  62. dt.Columns.Add("PageIndex");
  63. dt.Columns.Add("PageSize");
  64. dt.Columns.Add("RecordCount");
  65. dt.Rows.Add();
  66. dt.Rows[0]["PageIndex"] = pageIndex;
  67. dt.Rows[0]["PageSize"] = PageSize;
  68. dt.Rows[0]["RecordCount"] = cmd.Parameters["@RecordCount"].Value;
  69. ds.Tables.Add(dt);
  70. return ds;
  71. }
  72. }
  73. }
Now run the application:

application
Image 4.

run the application
Image 5.

output
Image 6.

next
Image 7.