1.前台代码html和js代码:
2.前台html与js代码
//请求数据 function InitTable(pageIndex) { $.ajax({ type: "POST", dataType: "text", url: 'Handler/PagerHandler.ashx', //提交到一般处理程序请求数据 data: "pageIndex=" + (pageIndex + 1) + "&pageSize=" + pageSize, //提交两个参数:pageIndex(页面索引),pageSize(显示条数) success: function(data) { $("#Result tr:gt(1)").remove(); //移除Id为Result的表格里的行,从第二行开始(这里根据页面布局不同页变) $("#Result").append(data); //将返回的数据追加到表格 } }); }
Dawn----无刷新分页
页面后台代码:
public string pageCount = string.Empty; //总条目数
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pageCount = Common.personList.Count.ToString();
}
}
2.后台handler代码
using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class PagerHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string str = string.Empty;
//具体的页面数
int pageIndex;
int.TryParse(context.Request["pageIndex"], out pageIndex);
//页面显示条数
int size = Convert.ToInt32(context.Request["pageSize"]);
if (pageIndex == 0)
{
pageIndex = 1;
}
System.Collections.Generic.IEnumerable list = Common.personList.Skip((pageIndex - 1) * size).Take(size);
StringBuilder sb = new StringBuilder();
foreach (Person p in list)
{
sb.Append("");
sb.Append(p.Id.ToString());
sb.Append("");
sb.Append(p.Name);
sb.Append("");
}
str = sb.ToString();
context.Response.Write(str);
}
public bool IsReusable {
get {
return false;
}
}
}
3.创建数据源代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
///
/// 实体类---人
///
[Serializable]
public class Person
{
///
/// 编号
///
public int Id { get; set; }
///
/// 名字
///
public string Name { get; set; }
}
///
///Common 的摘要说明
///
public class Common
{
public static List personList = new List();
static Common()
{
for (int i = 0; i < 555; i++)
{
personList.Add(new Person
{
Id = Convert.ToInt32("100" + i.ToString()),
Name = "PersonName_" + i.ToString()
});
}
}
}