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].[GetStudentData]
  2. (
  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, Country INTO #Results FROM Student
  18. SELECT*FROM #Results
  19. WHERE RowNumber BETWEEN(@PageIndex -1)* @PageSize + 1 AND(((@PageIndex -1)* @PageSize + 1)+ @PageSize)- 1
  20. SELECT @RecordCount =COUNT(*)FROM #Results
  21. DROPTABLE #Results
  22. END
The following is my aspx code:
  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="jQueryPagination.Default"%>
  2. <!DOCTYPEhtml>
  3. <htmlxmlns="http://www.w3.org/1999/xhtml">
  4. <headrunat="server">
  5. <title></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. $(document).ready(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. functionGetStudents(pageIndex) {
  19. $.ajax({
  20. type: "POST",
  21. url: "Default.aspx/GetStudents",
  22. data: '{pageIndex: ' + pageIndex + '}',
  23. contentType: "application/json; charset=utf-8",
  24. dataType: "json",
  25. success: OnSuccess,
  26. failure: function (response) {
  27. alert(response.d);
  28. },
  29. error: function (response) {
  30. alert(response.d);
  31. }
  32. });
  33. }
  34. var row;
  35. functionOnSuccess(response) {
  36. varxmlDoc = $.parseXML(response.d);
  37. var xml = $(xmlDoc);
  38. var students = xml.find("Student");
  39. if (row == null) {
  40. row = $("[id*=GridViewStudent] tr:last-child").clone(true);
  41. }
  42. $("[id*=GridViewStudent] tr").not($("[id*=GridViewStudent] tr:first-child")).remove();
  43. if (students.length> 0) {
  44. $.each(students, function () {
  45. var student = $(this);
  46. $("td", row).eq(0).html($(this).find("Name").text());
  47. $("td", row).eq(1).html($(this).find("Email").text());
  48. $("td", row).eq(2).html($(this).find("Class").text());
  49. $("td", row).eq(3).html($(this).find("EnrollYear").text());
  50. $("td", row).eq(4).html($(this).find("City").text());
  51. $("td", row).eq(5).html($(this).find("Country").text());
  52. $("[id*=GridViewStudent]").append(row);
  53. row = $("[id*=GridViewStudent] tr:last-child").clone(true);
  54. });
  55. var pager = xml.find("dtForPaging");
  56. $(".Pagination").jQ_Pager({
  57. ActiveCssClass: "current",
  58. PagerCssClass: "pager",
  59. PageIndex: parseInt(pager.find("PageIndex").text()),
  60. PageSize: parseInt(pager.find("PageSize").text()),
  61. RecordCount: parseInt(pager.find("RecordCount").text())
  62. });
  63. $(".Name").each(function () {
  64. varsearchPattern = newRegExp('(' + SearchTerm() + ')', 'ig');
  65. $(this).html($(this).text().replace(searchPattern, "<span class = 'highlight'>" + SearchTerm() + "</span>"));
  66. });
  67. }
  68. else {
  69. varempty_row = row.clone(true);
  70. $("td:first-child", empty_row).attr("colspan", $("td", row).length);
  71. $("td:first-child", empty_row).attr("align", "center");
  72. $("td:first-child", empty_row).html("No records found for the search criteria.");
  73. $("td", empty_row).not($("td:first-child", empty_row)).remove();
  74. $("[id*=GridViewStudent]").append(empty_row);
  75. }
  76. };
  77. </script>
  78. </head>
  79. <body>
  80. <formid="form1"runat="server">
  81. <div>
  82. <tablestyle="border: solid15pxblue; width: 100%; vertical-align: central;">
  83. <tr>
  84. <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: Display Records With Paging in ASP.NET Grid View using jQuery</td>
  85. </tr>
  86. <tr>
  87. <td>
  88. <tablestyle="width: 80%; text-align: center; vertical-align: central;">
  89. <tr>
  90. <tdstyle="text-align: left;">
  91. <asp:GridViewID="GridViewStudent"runat="server"AutoGenerateColumns="False"Width="100%"
  92. BackColor="#DEBA84"BorderColor="#DEBA84"BorderStyle="None"BorderWidth="1px"CellPadding="3"CellSpacing="2">
  93. <Columns>
  94. <asp:BoundFieldDataField="Name"HeaderText="Student Name"HeaderStyle-HorizontalAlign="Left"></asp:BoundField>
  95. <asp:BoundFieldDataField="Email"HeaderText="Email"HeaderStyle-HorizontalAlign="Left"/>
  96. <asp:BoundFieldDataField="Class"HeaderText="Class"HeaderStyle-HorizontalAlign="Left"/>
  97. <asp:BoundFieldDataField="EnrollYear"HeaderText="Enroll Year"HeaderStyle-HorizontalAlign="Left"/>
  98. <asp:BoundFieldDataField="City"HeaderText="City"HeaderStyle-HorizontalAlign="Left"/>
  99. <asp:BoundFieldDataField="Country"HeaderText="Country"HeaderStyle-HorizontalAlign="Left"/>
  100. </Columns>
  101. <FooterStyleBackColor="#F7DFB5"ForeColor="#8C4510"/>
  102. <HeaderStyleBackColor="#A55129"Font-Bold="True"ForeColor="White"/>
  103. <PagerStyleForeColor="#8C4510"HorizontalAlign="Center"/>
  104. <RowStyleBackColor="#FFF7E7"ForeColor="#8C4510"/>
  105. <SelectedRowStyleBackColor="#738A9C"Font-Bold="True"ForeColor="White"/>
  106. <SortedAscendingCellStyleBackColor="#FFF1D4"/>
  107. <SortedAscendingHeaderStyleBackColor="#B95C30"/>
  108. <SortedDescendingCellStyleBackColor="#F1E5CE"/>
  109. <SortedDescendingHeaderStyleBackColor="#93451F"/>
  110. </asp:GridView>
  111. </td>
  112. </tr>
  113. <tr>
  114. <td>
  115. <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;">
  116. </div>
  117. </td>
  118. </tr>
  119. </table>
  120. </td>
  121. </tr>
  122. </table>
  123. </div>
  124. </form>
  125. </body>
  126. </html>
Now My aspx.cs code is:
  1. using System;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Data;
  4. usingSystem.Data.SqlClient;
  5. usingSystem.Linq;
  6. usingSystem.Web;
  7. usingSystem.Web.Services;
  8. usingSystem.Web.UI;
  9. usingSystem.Web.UI.WebControls;
  10. namespacejQueryPagination
  11. {
  12. publicpartialclassDefault : System.Web.UI.Page
  13. {
  14. privatestaticintPageSize = 5;
  15. protectedvoidPage_Load(object sender, EventArgs e)
  16. {
  17. if (!IsPostBack)
  18. {
  19. BindGridViewHeader();
  20. }
  21. }
  22. privatevoidBindGridViewHeader()
  23. {
  24. DataTabledtHeader = newDataTable();
  25. dtHeader.Columns.Add("Name");
  26. dtHeader.Columns.Add("Email");
  27. dtHeader.Columns.Add("Class");
  28. dtHeader.Columns.Add("EnrollYear");
  29. dtHeader.Columns.Add("City");
  30. dtHeader.Columns.Add("Country");
  31. dtHeader.Rows.Add();
  32. GridViewStudent.DataSource = dtHeader;
  33. GridViewStudent.DataBind();
  34. }
  35. [WebMethod]
  36. publicstaticstringGetStudents(intpageIndex)
  37. {
  38. stringSP_Name = "[GetStudentData]";
  39. SqlCommandcmd = newSqlCommand(SP_Name);
  40. cmd.CommandType = CommandType.StoredProcedure;
  41. cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
  42. cmd.Parameters.AddWithValue("@PageSize", PageSize);
  43. cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
  44. returnGetStudentData(cmd, pageIndex).GetXml();
  45. }
  46. privatestaticDataSetGetStudentData(SqlCommandcmd, intpageIndex)
  47. {
  48. SqlDataAdapter da;
  49. DataSet ds = newDataSet();
  50. SqlConnection con = newSqlConnection();
  51. ds = newDataSet();
  52. con.ConnectionString = @"Data Source=MyPC\SqlServer2k8; Initial Catalog=SchoolManagement; Integrated Security=true;";
  53. cmd.Connection = con;
  54. da = newSqlDataAdapter(cmd);
  55. da.Fill(ds, "Student");
  56. con.Open();
  57. cmd.ExecuteNonQuery();
  58. con.Close();
  59. //Addning Table For Paging Data
  60. DataTabledt = newDataTable("dtForPaging");
  61. dt.Columns.Add("PageIndex");
  62. dt.Columns.Add("PageSize");
  63. dt.Columns.Add("RecordCount");
  64. dt.Rows.Add();
  65. dt.Rows[0]["PageIndex"] = pageIndex;
  66. dt.Rows[0]["PageSize"] = PageSize;
  67. dt.Rows[0]["RecordCount"] = cmd.Parameters["@RecordCount"].Value;
  68. ds.Tables.Add(dt);
  69. return ds;
  70. }
  71. }
  72. }
Now run the application:

display record
Image 4.

paging
Image 5.

run the application
Image 6.