码迷,mamicode.com
首页 > 其他好文 > 详细

Ajax无刷新分页

时间:2014-08-12 13:26:24      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:blog   http   java   os   io   数据   for   ar   

<!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>
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

</head>
<body>
<ul id="ulcomment"></ul>
<table>
<tr id="trPage"></tr>
</table>
<script type="text/javascript">
    $.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.PostDate + "评论" + comment.Msg + "</li>");
                        $("#ulcomment").append(li);
                    }
                });
        });
    });
       $.post("PagedService.ashx", { "action": "getpagedata", "pagenum": "1" },//初始化的默认第一页
                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.PostDate + "评论" + comment.Msg + "</li>");
                        $("#ulcomment").append(li);
                    }
                })

    </script>
</body>
   
</html>

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ajax翻页.DAL.DataSetCommentTableAdapters;
using System.Web.Script.Serialization;

namespace Ajax翻页
{
    /// <summary>
    /// PagedService 的摘要说明
    /// </summary>
    public class PagedService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["action"];
            if (action=="getpagecount")
            {
                var adapter = new T_CommentsTableAdapter();
                int count = adapter.SelectCount().Value;
                int pagecount = count / 10;
                if (count%10!=0)
                {
                    pagecount++;
                }
                context.Response.Write(pagecount);    
            }
            else if (action == "getpagedata")
            {
                string getpagedata = context.Request["pagenum"];//获得传过来的页数
                int IPageNUm = Convert.ToInt32(getpagedata);
                var adapter = new T_CommentsTableAdapter();//获得adapter
                var data = adapter.GetPagedData((IPageNUm - 1) * 10 + 1, (IPageNUm) * 10);//根据页数获得数据
                List<Comment> list = new List<Comment>();//创建list
                foreach (var item in data)//在list中循环输入Comment属性PostDate,Msg  
                {
                    list.Add(new Comment() { PostDate = item.PostDate.ToShortDateString(), Msg=item.Msg });
                }
                JavaScriptSerializer jss = new JavaScriptSerializer();//序列化list为json
                context.Response.Write(jss.Serialize(list));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    public class Comment
    {
        public string PostDate { get; set; }
        public string Msg { get; set; }
    }
}

  

select *from(
SELECT Id, PostDate, Msg,Row_Number()over(order by PostDate)rownum FROM dbo.T_Comments
)t
where t.rownum>=@startRowIndex and t.rownum<@endRowIndex

 

Ajax无刷新分页,布布扣,bubuko.com

Ajax无刷新分页

标签:blog   http   java   os   io   数据   for   ar   

原文地址:http://www.cnblogs.com/blackHorseplan/p/3907010.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!