asp.net中利用Jquery+Ajax+Json实现无刷新分页(二)

2016-12-30 09:56:34来源:oschina作者:深圳大道人点击

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageTest.aspx.cs" Inherits="renmai_PageTest" %>



Jquery+Ajax分页测试
<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 pageCount = json.table2[0].pagecount;
var html = "";
$.each(json.table1, function (index, item) {
//循环获取数据
var id = item.Id;
var name = item.Name;
var sex = item.Sex;
html += "" + id + "" + name + "" + sex + "";
});
$("#Result").append(html);
if (pageIndex >= parseInt(pageCount)) {
$(".nextPage").hide();
}
else {
$("#nextpage").val(parseInt(pageIndex) + 1)
}
}
});
};
</script>












编号

姓名

性别

下一页



<%@ 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; }
#endregionDataSet 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 <= 58; i++)
{
dt.Rows.Add(i, "张三" + i.ToString().PadLeft(4, '0'));
}
RecordCount = dt.Rows.Count;
return dt;
}
#endregion ///
/// 对DataTable进行分页,起始页为1
///

///
///
///
///
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;
}
///
/// 获取总页数
///

/// 结果集数量
/// 页面数量
///
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;
}
}
}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台