asp.net中利用Jquery+Ajax+Json实现无刷新分页(二)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageTest.aspx.cs" Inherits="renmai_PageTest" %> <!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>Jquery+Ajax分页测试</title> <script src="../js/jquery-1.11.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { InitPage(1, 10); $(".nextPage").click(function () { InitPage($("#nextpage").val(), 10); }); }); //请求数据 function InitPage(pageIndex, pageSize) { $.ajax({ type: "POST", dataType: "json", url: '/Handler/Handler1.ashx', data: "type=show&random=" + Math.random() + "&pageIndex=" + pageIndex + "&pageSize=" + pageSize, error: function () { alert('error data'); }, //错误执行方法 success: function (data) { var json = data; //数组 var recordCount = json.table2[0].recordcount; var html = ""; $.each(json.table1, function (index, item) { //循环获取数据 var id = item.Id; var name = item.Name; var sex = item.Sex; html += "<tr><td>" + id + "</td><td>" + name + "</td><td>" + sex + "</td></tr>"; }); $("#Result").append(html); $("#nextpage").val(parseInt(pageIndex) + 1); } }); }; </script> </head> <body> <form id="form1" runat="server"> <div> <input type="hidden" id="nextpage" value="1" /> <table id="Result" cellspacing="0" cellpadding="0"> <tr> <th style="width:50px;"> 编号 </th> <th style="width:100px;"> 姓名 </th> <th style="width:50px;"> 性别 </th> </tr> </table> <a href="javascript:void(0)" class="nextPage">下一页</a> </div> </form> </body> </html>
<%@ WebHandler Language="C#" Class="Handler1" %> using System; using System.Web; using System.Data; using Newtonsoft.Json; public class Handler1 : IHttpHandler { /* Handler/Handler1.ashx */ protected static int RecordCount = 0; protected static DataTable dt = CreateDT(); public void ProcessRequest (HttpContext context) { string type=context.Request["type"]; int pageIndex = MSCL.ConvertHelper.ObjectToInt(context.Request["pageIndex"]); int pageSize = MSCL.ConvertHelper.ObjectToInt(context.Request["pageSize"]); switch (type) { case "show": #region 分页配置 if (pageIndex == 0) { pageIndex = 1; } #endregion DataSet ds = new DataSet(); DataTable dtData = GetPagedTable(dt, pageIndex, pageSize); dtData.TableName = "table1"; DataTable dtCount = new DataTable(); dtCount.TableName = "table2"; dtCount.Columns.Add("recordcount"); dtCount.Rows.Add(dt.Rows.Count); ds.Tables.Add(dtData); ds.Tables.Add(dtCount); string json = JsonConvert.SerializeObject(ds, Formatting.Indented); context.Response.Write(json); break; default : break; } context.Response.End(); } #region 模拟数据 private static DataTable CreateDT() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("Id", typeof(int)) { DefaultValue = 0 }); dt.Columns.Add(new DataColumn("Name", typeof(string)) { DefaultValue = "1" }); dt.Columns.Add(new DataColumn("Sex", typeof(string)) { DefaultValue = "男" }); for (int i = 1; i <= 1000; i++) { dt.Rows.Add(i, "张三" + i.ToString().PadLeft(4, '0')); } RecordCount = dt.Rows.Count; return dt; } #endregion /// <summary> /// 对DataTable进行分页,起始页为1 /// </summary> /// <param name="dt"></param> /// <param name="PageIndex"></param> /// <param name="PageSize"></param> /// <returns></returns> public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize) { if (PageIndex == 0) return dt; DataTable newdt = dt.Copy(); newdt.Clear(); int rowbegin = (PageIndex - 1) * PageSize; int rowend = PageIndex * PageSize; if (rowbegin >= dt.Rows.Count) return newdt; if (rowend > dt.Rows.Count) rowend = dt.Rows.Count; for (int i = rowbegin; i <= rowend - 1; i++) { DataRow newdr = newdt.NewRow(); DataRow dr = dt.Rows[i]; foreach (DataColumn column in dt.Columns) { newdr[column.ColumnName] = dr[column.ColumnName]; } newdt.Rows.Add(newdr); } return newdt; } /// <summary> /// 获取总页数 /// </summary> /// <param name="sumCount">结果集数量</param> /// <param name="pageSize">页面数量</param> /// <returns></returns> public static int getPageCount(int sumCount, int pageSize) { int page = sumCount / pageSize; if (sumCount % pageSize > 0) { page = page + 1; } return page; } public bool IsReusable { get { return false; } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。