无刷新 分页评论
服务器端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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 pagenum = context.Request [ "pagenum" ]; int iPageNum = Convert.ToInt32 (pagenum); var adapter = new T_CommentsTableAdapter(); var data = adapter.GetPageData((iPageNum - 1 ) * 10+1 ,iPageNum*10); //得到所在页的评论 //新建Comment类的List List <Comment> list = new List< Comment>(); foreach ( var row in data ) { list .Add( new Comment() { PostDate = row.PostDate.ToShortDateString() , Msg = row.Msg }); } JavaScriptSerializer jss = new JavaScriptSerializer(); context .Response.Write(jss .Serialize(list)); //转化成简单的Comment对象以后再进行序列化 } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <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); 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); } }); }); } }); }); </script> </head > <body > <ul id = "ulComment" ></ul > <table > <tr id = "trPage" ></tr > </table > |
附件列表