前端首先添加加载更多按钮,使用ajax异步获取后台数据,成功获取数据后,如果后台数据大于自定义条数,则拼接文章列表页板块布局,否则提示暂无更多数据。
<div class="load" id="load"><a href="javascript:loadMore();">加载更多</a></div> <script type="text/javascript"> var pageindex = 1; //页数 var pagesize = 8; //每页条数 function loadMore() { $.ajax({ url: '/dev/ClickLoad.ashx', type: 'POST', dataType: 'json', data: { action: 'more', pageindex: ++pageindex, pagesize: pagesize, cid: '<%=category_id%>' }, success: function(d) { if(d.code == 0) { var str = ""; jQuery.each(d.data, function(i, o) { var onlyDate =o.add_time.substring(0,10); str += '<li>' + '<div class="photo_box">' + '<a href="bulletinboard_show.aspx?id='+o.id+'" title="' + o.title + '">' + '<i>'+'</i>' + '<span>' +o.title+ '</span>' + '</a>' + '<em>' +onlyDate+ '</em>' + '</li>'; }) $("#all").append(str); if(d.datanum < pagesize) { $('#load').html("暂无更多数据"); } } else { $('#load').html("暂无更多数据"); } }, error: function(e) {} }) } </script>逻辑处理
<%@ WebHandler Language="C#" Class="DTcms.Web.dev.ClickLoad" %> using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Text; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using DTcms.Common; using DTcms.DBUtility; using NetWing.Common.Data.SQLServer; using NetWing.Common.Request; using NetWing.Common; namespace DTcms.Web.dev { /// <summary> /// dev 的摘要说明 /// </summary> public class ClickLoad : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string action = context.Request["action"]; if (string.IsNullOrEmpty(action)) { context.Response.Write("{\"code\":1000, \"msg\":\"请求失败,参数有误!\"}"); return; } switch (action) { case "more": GetMore(context); break; } } public void GetMore(HttpContext context) { int pageindex = int.Parse(context.Request["pageindex"]);//当前第几页 int pagesize = int.Parse(context.Request["pagesize"]);//每页记录条数 string cid = context.Request["cid"]; string strWSQL = "1=1"; if (!string.IsNullOrEmpty(cid)) { strWSQL = " category_id=" + cid; } string sqls = "select count(id) from dt_article where " + strWSQL + ""; int allsize = (int)SqlEasy.ExecuteScalar(sqls);//总条数 int allpagesiza = 0; decimal de = 0M; if (allsize != 0) { de = Convert.ToDecimal(allsize) / Convert.ToDecimal(pagesize); allpagesiza = (int)Math.Ceiling(de); } int pagenum = (pageindex - 1) * pagesize; DataTable dt = SqlEasy.ExecuteDataTable("select top " + pagesize + " * from(select row_number() over(order by sort_id asc,add_time desc,id desc) as rownumber,* from dt_article where " + strWSQL + ") A where rownumber > " + pagenum + ""); if (dt.Rows.Count > 0) { context.Response.Write("{\"code\":0,\"msg\":\"获取成功!\",\"data\":" + JSONhelper.ToJson(dt, false) + ",\"datanum\":" + dt.Rows.Count + "}"); } else { context.Response.Write("{\"code\":1000,\"msg\":\"暂无数据!\"}"); } } public bool IsReusable { get { return false; } } } }