c#与jquery实现局部刷新分页的代码
本文导语: 1、ashx代码部分 代码示例: using System; using System.Web; using System.Data; using System.Web.Script.Serialization; using System.Collections.Generic; //using System.Linq; public class page : IHttpHandler { public void ProcessRequest (HttpContext context) { cont...
1、ashx代码部分
using System;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;
using System.Collections.Generic;
//using System.Linq;
public class page : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string action = context.Request["action"].ToString(); //接受前台传来的action的值
//---------------------
//datatable数据库内容
//---------------------
DataTable newdtb = new DataTable();
newdtb.Columns.Add("Id", typeof(int));
newdtb.Columns.Add("centent", typeof(string));
newdtb.Columns["Id"].AutoIncrement = true;
for (int i = 1; i < 20; i++)
{
DataRow newRow = newdtb.NewRow();
newRow["centent"] = "我要成功,我要奋斗,我要有自己的事业";
newdtb.Rows.Add(newRow);
}
if (action == "main")
{
int number = Convert.ToInt32(context.Request["number"]); //前台传过来的数据,就是第几页
int start = (number - 1) * 5; //计算出从哪行开始查询
int end = start + 4; //计算出查询到哪行结束
List ls = new List();
for (int i = start; i < end; i++)
{
pageFen pf = new pageFen();
pf.id = Convert.ToInt32(newdtb.Rows[i]["Id"]);
pf.centent = newdtb.Rows[i]["centent"].ToString();
ls.Add(pf);
}
JavaScriptSerializer jss = new JavaScriptSerializer();
context.Response.Write(jss.Serialize(ls));
}
//得到数据需要分几页
if (action == "page")
{
int count = Convert.ToInt32(newdtb.Rows.Count);
int page = count / 5;
if (count % 5 != 0)
{
page++;
}
context.Response.Write(page);
}
}
public class pageFen
{
public int id { get; set; }
public string centent { get; set; }
}
public bool IsReusable {
get {
return false;
}
}
}
2、js与html内容部分
/**
* jquery 局部刷新分页
* edit www.
*/
$(function () {
$.post("page.ashx", { "action": "page" }, function (data, status) {
if (status == "success") {
for (var i = 1; i