AJAX进行分页

新建数据集:PagingDataSet.xsd

SELECT * from 
( select id, areaID, area, father,Row_Number() over (order by areaID) rownum FROM dbo.area) t
where t.rownum >=@startRowIndex and t.rownum <=@endRowIndex

在集合中添加两个参数: startRowIndex  endRowIndex

一般处理程序:PagedService.ashx

namespace ajaxApp.service
{
    /// <summary>
    /// PagedService 的摘要说明
    /// </summary>
    public class PagedService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string action = context.Request["action"];
            if (action == "getPagecount")
            {
                var adapter = new areaTableAdapter();
                int count = adapter.SelectCount().Value;
                int pageCount = count / 10;
                if (count % 10 != 0)
                {
                    pageCount++;
                }
                context.Response.Write(pageCount);
            }
            else if (action == "getPagedata")
            {
                string pagenum = context.Request["pagenum"];
                int iPageNum = Convert.ToInt32(pagenum);
                var adapter = new areaTableAdapter();
                var data = adapter.GetPagedData((iPageNum - 1) * 10 + 1, (iPageNum) * 10);
                List<Area> list = new List<Area>();
                foreach (var row in data)
                {
                    list.Add(new Area() { id=row.id,areaID=row.areaID,area=row.area,father=row.father});
                }
                //用LINQ更简单
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(list));

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    public class Area
    {
        public long id { get; set; }
        public int areaID { get; set; }
        public string area { get; set; }
        public int father { get; set; }
    }
}

前台页面:Paged.htm

<head>
    <title></title>
    <script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $.post("PagedService.ashx", { "action": "getPagecount" }, function (data, status) {
                for (var i = 1; i <= data; i++) {
                    var td = $("<td><a href=''>" + i + "</a></td>");
                    $("#trPage").append(td);
                }

                $("#trPage td").click(function (e) {
                    e.preventDefault();
                    $.post("PagedService.ashx",{"action":"getPagedata","pagenum":$(this).text()},
                    function(data,status){
                    var comments = $.parseJSON(data);
                    $("#ulComment").empty();
                    for(var i =0;i<comments.length;i++){
                    var comment = comments[i];
                    var li = $("<li>"+comment.id+":"+comment.areaID+":"+comment.area+":"+comment.father+"</li>");
                    $("#ulComment").append(li);
                    }
                    });
                });

            });
       })
    </script>
</head>
<body>
<ul id="ulComment"></ul>
<table>
<tr id="trPage"></tr>
</table>
</body>
</html>
原文地址:https://www.cnblogs.com/kennyliu/p/3442503.html