Figure 1 shows the DataTable in design mode from which I am reading records.

table design
Figure 1.

The following is the script of My Employee table:

  1. CREATE TABLE [dbo].[Employee](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Email] [varchar](500) NULL,
  5. [Country] [varchar](50) NULL,
  6. [ProjectID] [int] NULL,
  7. [ManagerName] [varchar](50) NULL,
  8. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  9. (
  10. [ID] ASC
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  12. ) ON [PRIMARY]
  13. GO
To do this I created a Stored Procedure as in the following:
  1. CREATE PROCEDURE [dbo].[GetEmployee_SQLPaging]
  2. @PageIndex INT = 1
  3. ,@PageSize INT = 10
  4. ,@RecordCount INT OUTPUT
  5. AS
  6. BEGIN
  7. SET NOCOUNT ON;
  8. SELECT ROW_NUMBER() OVER
  9. (
  10. ORDER BY [ID] ASC
  11. )AS RowNumber
  12. ,[ID],[Name],[Email],[Country] INTO #Results FROM [Employee]
  13. SELECT @RecordCount = COUNT(*) FROM #Results
  14. SELECT * FROM #Results
  15. WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
  16. DROP TABLE #Results
  17. END
SQL Query
Figure 2.

Now the following is my aspx:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="JSON_GridView_SQLPaging.Default" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <style type="text/css">
  6. body {
  7. font-family: Arial;
  8. font-size: 10pt;
  9. }
  10. .Pager span {
  11. text-align: center;
  12. color: navy;
  13. display: inline-block;
  14. width: 20px;
  15. background-color: red;
  16. margin-right: 3px;
  17. line-height: 150%;
  18. border: 2px solid navy;
  19. }
  20. .Pager a {
  21. text-align: center;
  22. display: inline-block;
  23. width: 20px;
  24. background-color: green;
  25. color: #fff;
  26. border: 1px solid #3AC0F2;
  27. margin-right: 3px;
  28. line-height: 150%;
  29. text-decoration: none;
  30. }
  31. </style>
  32. <title></title>
  33. <script src="Scripts/jquery.min.js"></script>
  34. <script src="jQueryPager.min.js" type="text/javascript"></script>
  35. <script type="text/javascript">
  36. $(function () {
  37. BindEmployee(1);
  38. });
  39. $(".Pager .page").live("click", function () {
  40. BindEmployee(parseInt($(this).attr('page')));
  41. });
  42. function BindEmployee(pageIndex) {
  43. $.ajax({
  44. type: "POST",
  45. url: "Default.aspx/BindEmployee",
  46. data: '{pageIndex: ' + pageIndex + '}',
  47. contentType: "application/json; charset=utf-8",
  48. dataType: "json",
  49. success: OnSuccess,
  50. failure: function (response) {
  51. alert(response.d);
  52. },
  53. error: function (response) {
  54. alert(response.d);
  55. }
  56. });
  57. }
  58. function OnSuccess(response) {
  59. var xmlDoc = $.parseXML(response.d);
  60. var xml = $(xmlDoc);
  61. var emp = xml.find("Employee");
  62. var row = $("[id*=gvEmployee] tr:last-child").clone(true);
  63. $("[id*=gvEmployee] tr").not($("[id*=gvEmployee] tr:first-child")).remove();
  64. $.each(emp, function () {
  65. var employee = $(this);
  66. $("td", row).eq(0).html($(this).find("ID").text());
  67. $("td", row).eq(1).html($(this).find("Name").text());
  68. $("td", row).eq(2).html($(this).find("Email").text());
  69. $("td", row).eq(3).html($(this).find("Country").text());
  70. $("[id*=gvEmployee]").append(row);
  71. row = $("[id*=gvEmployee] tr:last-child").clone(true);
  72. });
  73. var pager = xml.find("Pager");
  74. $(".Pager").jQueryPagerFunc({
  75. ActiveCssClass: "current",
  76. PagerCssClass: "pager",
  77. PageIndex: parseInt(pager.find("PageIndex").text()),
  78. PageSize: parseInt(pager.find("PageSize").text()),
  79. RecordCount: parseInt(pager.find("RecordCount").text())
  80. });
  81. };
  82. </script>
  83. </head>
  84. <body>
  85. <form id="form1" runat="server">
  86. <div>
  87. <table style="width: 100%; text-align: center; border: solid 5px red; background-color: blue; vertical-align: top;">
  88. <tr>
  89. <td>
  90. <div>
  91. <fieldset style="width: 99%;">
  92. <legend style="font-size: 20pt; color: white; font-family: Verdana">jQuery JSON - Showing Records in Grid View With SQL Paging</legend>
  93. <table style="width: 100%;">
  94. <tr>
  95. <td style="vertical-align: top; background-color: #9DD1F1; text-align: center;">
  96. <asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="false"
  97. HeaderStyle-ForeColor="White" Width="100%" BackColor="Yellow">
  98. <Columns>
  99. <asp:BoundField ItemStyle-Width="30px" DataField="ID" HeaderText="ID" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-ForeColor="Red" />
  100. <asp:BoundField ItemStyle-Width="80px" DataField="Name" HeaderText="Name" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-ForeColor="Red" />
  101. <asp:BoundField ItemStyle-Width="100px" DataField="Email" HeaderText="Email" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-ForeColor="Red" />
  102. <asp:BoundField ItemStyle-Width="80px" DataField="Country" HeaderText="City" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-ForeColor="Red" />
  103. </Columns>
  104. <HeaderStyle BackColor="Red" HorizontalAlign="Left" />
  105. </asp:GridView>
  106. <br />
  107. <div class="Pager" style="background-color: green; padding-top: 10px; padding-bottom: 10px;"></div>
  108. </td>
  109. </tr>
  110. </table>
  111. </fieldset>
  112. </div>
  113. </td>
  114. </tr>
  115. </table>
  116. </div>
  117. </form>
  118. </body>
  119. </html>
Now the following is the aspx.cs code:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Web.Services;
  9. using System.Configuration;
  10. using System.Data.SqlClient;
  11. namespace JSON_GridView_SQLPaging
  12. {
  13. public partial class Default : System.Web.UI.Page
  14. {
  15. private static int PageSize = 10;
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. if (!IsPostBack)
  19. {
  20. BindDummyRowToGridView();
  21. }
  22. }
  23. private void BindDummyRowToGridView()
  24. {
  25. DataTable dummy = new DataTable();
  26. dummy.Columns.Add("ID");
  27. dummy.Columns.Add("Name");
  28. dummy.Columns.Add("Email");
  29. dummy.Columns.Add("Country");
  30. dummy.Rows.Add();
  31. gvEmployee.DataSource = dummy;
  32. gvEmployee.DataBind();
  33. }
  34. [WebMethod]
  35. public static string BindEmployee(int pageIndex)
  36. {
  37. string query = "[GetEmployee_SQLPaging]";
  38. SqlCommand cmd = new SqlCommand(query);
  39. cmd.CommandType = CommandType.StoredProcedure;
  40. cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
  41. cmd.Parameters.AddWithValue("@PageSize", PageSize);
  42. cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
  43. return ReadData(cmd, pageIndex).GetXml();
  44. }
  45. private static DataSet ReadData(SqlCommand cmd, int pageIndex)
  46. {
  47. string connectionString = @"Data Source=INDIA\MSSQLServer2k8; Initial Catalog= TestDB; Integrated Security=true;";
  48. using (SqlConnection con = new SqlConnection(connectionString))
  49. {
  50. using (SqlDataAdapter sda = new SqlDataAdapter())
  51. {
  52. cmd.Connection = con;
  53. sda.SelectCommand = cmd;
  54. using (DataSet ds = new DataSet())
  55. {
  56. sda.Fill(ds, "Employee");
  57. DataTable dt = new DataTable("Pager");
  58. dt.Columns.Add("PageIndex");
  59. dt.Columns.Add("PageSize");
  60. dt.Columns.Add("RecordCount");
  61. dt.Rows.Add();
  62. dt.Rows[0]["PageIndex"] = pageIndex;
  63. dt.Rows[0]["PageSize"] = PageSize;
  64. dt.Rows[0]["RecordCount"] = cmd.Parameters["@RecordCount"].Value;
  65. ds.Tables.Add(dt);
  66. return ds;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
For this I created JavaScript file jQueryPager.min.js as in the following:
  1. function jQueryPagerFunc(a, b)
  2. {
  3. var c = '<a style = "cursor:pointer" class="page" page = "{1}">{0}</a>';
  4. var d = "<span>{0}</span>";
  5. var e, f, g;
  6. var g = 5;
  7. var h = Math.ceil(b.RecordCount / b.PageSize);
  8. if (b.PageIndex > h)
  9. { b.PageIndex = h }
  10. var i = "";
  11. if (h > 1)
  12. {
  13. f = h > g ? g : h;
  14. e = b.PageIndex > 1 && b.PageIndex + g - 1 < g ? b.PageIndex : 1;
  15. if (b.PageIndex > g % 2)
  16. {
  17. if (b.PageIndex == 2) f = 5;
  18. else f = b.PageIndex + 2
  19. }
  20. else
  21. {
  22. f = g - b.PageIndex + 1
  23. }
  24. if (f - (g - 1) > e)
  25. {
  26. e = f - (g - 1)
  27. }
  28. if (f > h)
  29. {
  30. f = h;
  31. e = f - g + 1 > 0 ? f - g + 1 : 1
  32. }
  33. var j = (b.PageIndex - 1) * b.PageSize + 1;
  34. var k = j + b.PageSize - 1;
  35. if (k > b.RecordCount)
  36. {
  37. k = b.RecordCount
  38. }
  39. i = "<b>Records " + (j == 0 ? 1 : j) + " - " + k + " of " + b.RecordCount + "</b> ";
  40. if (b.PageIndex > 1)
  41. {
  42. i += c.replace("{0}", "<<").replace("{1}", "1");
  43. i += c.replace("{0}", "<").replace("{1}", b.PageIndex - 1)
  44. }
  45. for (var l = e; l <= f; l++)
  46. {
  47. if (l == b.PageIndex)
  48. {
  49. i += d.replace("{0}", l)
  50. }
  51. else
  52. {
  53. i += c.replace("{0}", l).replace("{1}", l)
  54. }
  55. }
  56. if (b.PageIndex < h)
  57. {
  58. i += c.replace("{0}", ">").replace("{1}", b.PageIndex + 1);
  59. i += c.replace("{0}", ">>").replace("{1}", h)
  60. }
  61. }
  62. a.html(i);
  63. try
  64. {
  65. a[0].disabled = false
  66. }
  67. catch (m)
  68. { }
  69. }
  70. (function (a)
  71. {
  72. a.fn.jQueryPagerFunc = function (b) {
  73. var c = {};
  74. var b = a.extend(c, b);
  75. return this.each(function () { jQueryPagerFunc(a(this), b) })
  76. }
  77. })
  78. (jQuery);
Now run your application.

first record
Figure 3.

second record
Figure 4.

third record
Figure 5.

forth record
Figure 6.