JqueryPager+ashx 实现分页

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="study._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" language="javascript" src="jquery-pager/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" language="javascript" src="jquery-pager/jquery.pager.js"></script>
    <link rel="Stylesheet" type="text/css" href="jquery-pager/Pager.css" />
    <script type="text/javascript">
        var pagecount;
        $(document).ready(function () {
            $.get("Handler.ashx", { type: 1, pagesize: 10, index: 1 }, function (data, textStatus) {
                pagecount = data;
                $("#pager").pager({ pagenumber: 1, pagecount: pagecount, buttonClickCallback: PageClick });
            });
            Go(1);
        });
        PageClick = function (pageclickednumber) {
            $("#pager").pager({ pagenumber: pageclickednumber, pagecount: pagecount, buttonClickCallback: PageClick });
            Go(pageclickednumber);
        }
        function Go(index) {
            $("#Content").html("");
            $.getJSON("Handler.ashx", { type: 0, pagesize: 10, index: index }, function (data) {
                $("#Content").append("<tr><th style='130px'>ID</th><th style='150px'>Name</th><th style='150px'>ClassName</th></tr>");
                $.each(data, function (i, k) {
                    $("#Content").append("<tr><td>" + data[i].id + "</td><td>" + data[i].Name + "</td><td>" + data[i].ClassName + "</td></tr>");
                })
            });
        }
     </script>


</head>
<body>
    <form id="form1" runat="server">
      <div style=" 100%" id="abc">
           <table id="Content" >                         
           </table>
           <div id="pager" ></div>
        </div>    
    </form>
</body>
</html>









  public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int pageSize = int.Parse(context.Request.Params["pagesize"]); //每页记录数
            int pageIndex = int.Parse(context.Request.Params["index"]);  //当前页索引    
            int type = int.Parse(context.Request.Params["type"]); //1为获取总页数,0为获取分页数据          

            if (type == 1)
            {
                int recordCount = GetRecordCount("select count(*) from MyTest");
                int pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(recordCount) / pageSize));
                string str = pageCount.ToString();
                context.Response.Write(str);
            }
            else
            {
                string sql = string.Format("select id,Name,ClassName from ( select row_number() over (order by id) as rowNum,* from MyTest) as t "
                    + " where rowNum>{0} and rowNum<={1}", (pageIndex - 1) * pageSize, pageIndex * pageSize);
                System.Data.DataTable dt = Getds(sql).Tables[0];
                string str = "[" + JsonHelper.DataTableToJSON(dt) + "]";
                context.Response.Write(str);
            }
        }
        public int GetRecordCount(string sql)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=test;Integrated Security=True");
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            return Convert.ToInt32(cmd.ExecuteScalar().ToString());
        }
        public DataSet Getds(string sql)
        {
            SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=test;Integrated Security=True");
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            return ds;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

原文地址:https://www.cnblogs.com/TNSSTAR/p/2642412.html