• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NET中实现Ajax分页

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在页面中指定一个div容器来接收动态生成的分页数据:

1 <div id="div_menu">
2 </div>

使用jQuery来请求并处理Json格式数据:

 1 //定义页码与页容量
 2         var pageIndex = 1;
 3         var pageSize = 15;
 4         var pageCount = 0;
 5         var recordCount = 0;
 6         AjaxGetData(pageIndex, pageSize);
 7         //Ajax获取数据
 8         function AjaxGetData(index, size) {
 9             $.ajax({
10                 url: "ProcessData.aspx",
11                 type: "Get",
12                 data: "pageindex=" + index + "&pagesize=" + size + "&rnd=" + new Date(),
13                 dataType: "json",
14                 success: function (data) {
15                     var htmlStr = "";
16                     htmlStr += "<table width=100%>";
17                     for (var i = 0; i < data.Exercise_object.length; i++) {
18                         htmlStr += "<tr><td  class='rr' onmouseover='javascript:onOver(this)' onmouseout='javascript:onOut(this)'onclick='javascript:onDown(this);'>";
19                         htmlStr += "<a href='voucher/Exercise_Detail.aspx?>;
20                         htmlStr += "第" + data.Exercise_object[i]._row_number + "题";
21                         htmlStr += "</a>";
22                         htmlStr += "</td></tr>";
23                     }
24                     htmlStr += "<tr style='text-align:center;'>";
25                     htmlStr += "<td>";
26                     recordCount = Number(data.Count);
27                     pageCount = Math.ceil(recordCount / pageSize);
28                     htmlStr += "共" + recordCount + "条记录&nbsp;&nbsp;共<span id='count'>" + pageCount + "</span>页&nbsp;&nbsp;&nbsp;&nbsp;";
29                     htmlStr += "<a href='javascript:void' onclick='GoToPrePage()' id='aPrePage' >前一页</a>&nbsp;&nbsp; ";
30                     htmlStr += "<a href='javascript:void' onclick='GoToNextPage()' id='aNextPage'>后一页</a>&nbsp;&nbsp; ";
31                     htmlStr += "</td>";
32                     htmlStr += "</tr>";
33                     htmlStr += "</table>";
34                     $("#div_menu").html(htmlStr);
35                 },
36                 error: function (XMLHttpRequest, textStatus, errorThrown) {
37                     alert(XMLHttpRequest);
38                     alert(textStatus);
39                     alert(errorThrown);
40                 }
41             });
42         }
43         //前一页
44         function GoToPrePage() {
45             pageIndex -= 1;
46             if (pageIndex < 1) {
47                 pageIndex = 1;
48                 return;
49             }
50             AjaxGetData(pageIndex, pageSize);
51         }
52         //后一页
53         function GoToNextPage() {
54             pageIndex += 1;
55             if (pageIndex > pageCount) {
56                 pageIndex = pageCount;
57                 return;
58             }
59             AjaxGetData(pageIndex, pageSize);
60         } 

新建一个一般处理程序,来处理Ajax的异步请求:

  1 private readonly BLL.D_Accounting_Entry_Exercise bll = new BLL.D_Accounting_Entry_Exercise();
  2         private string _action = "0";
  3         protected void Page_Load(object sender, EventArgs e)
  4         {
  5             Int32 pageIndex = Int32.MinValue;
  6             Int32 pageSize = Int32.MinValue;
  7 
  8             if (Request["action"] != null)
  9                 this._action = Request["action"];
 10 
 11             JavaScriptSerializer jss = new JavaScriptSerializer();
 12             if (Request["pageindex"] != null)
 13             {
 14                 pageIndex = Int32.Parse(Request["pageindex"].ToString());
 15                 pageSize = Request["pagesize"] != null ? Int32.Parse(Request["pagesize"].ToString()) : 10;
 16 
 17                 //处理接收到的数据
 18                 int start = 0;
 19                 int end = 0;
 20 
 21                 if (this._action == "0")
 22                 {
 23                     int recordCount = getAllCount();
 24                     int pageCount = (int)Math.Ceiling(((double)recordCount) / ((double)pageSize));
 25                     if (pageIndex > pageCount)
 26                     {
 27                         pageIndex = pageCount;
 28                     }
 29                     else if (pageIndex < 1)
 30                         pageIndex = 1;
 31                     start = (pageIndex - 1) * pageSize + 1;
 32                     end = pageIndex * pageSize;
 33 
 34                     IList<Exercise> exerciseLists = new List<Exercise>();
 35                     Exercise exercise = null;
 36                     DataSet set = GetDataFromDB(start, end);
 37                     int id = 0;
 38                     for (int i = 0; i < set.Tables[0].Rows.Count; i++)
 39                     {
 40                         //将第一行记录的ID存入Session
 41                         Session["first_id"] = set.Tables[0].Rows[0]["question_id"];
 42                         exercise = new Exercise();
 43                         id = Convert.ToInt32(set.Tables[0].Rows[i]["question_id"].ToString());
 44                         exercise._question_id = id;
 45                         exercise._question_content = set.Tables[0].Rows[i]["question_content"].ToString();
 46                         exercise._question_answer = set.Tables[0].Rows[i]["question_answer"].ToString();
 47                         exercise._question_analyze = set.Tables[0].Rows[i]["question_analyze"].ToString();
 48                         exercise._question_status = Convert.ToInt32(set.Tables[0].Rows[i]["question_status"].ToString());
 49                         exercise._user_id = Convert.ToInt32(set.Tables[0].Rows[i]["user_id"].ToString());
 50                         exercise._add_time = Convert.ToDateTime(set.Tables[0].Rows[i]["add_time"].ToString());
 51                         exercise._row_number = Convert.ToInt32(set.Tables[0].Rows[i]["Row"].ToString());
 52                         exerciseLists.Add(exercise);
 53                     }
 54                     if (exerciseLists.Count > 0)
 55                     {
 56                         Response.Write("{\"Count\":" + recordCount + ",\"Exercise_object\":" + jss.Serialize(exerciseLists) + "}");
 57                     }
 58                     else
 59                     {
 60                         Response.Write("{\"Count\":0,\"Exercise_object\":null}");
 61                     }
 62                     Response.End();
 63                 }
 64                 else if (this._action == "1")
 65                 {
 66                     string classID = Request["classid"];
 67                     string opSign = Request["opsign"];
 68                     int recordCount = GetYSPXCount(opSign, classID);
 69                     int pageCount = (int)Math.Ceiling(((double)recordCount) / ((double)pageSize));
 70                     if (pageIndex > pageCount)
 71                     {
 72                         pageIndex = pageCount;
 73                     }
 74                     else if (pageIndex < 1)
 75                         pageIndex = 1;
 76                     start = (pageIndex - 1) * pageSize + 1;
 77                     end = pageIndex * pageSize;
 78 
 79                     IList<operationModel> operList = new List<operationModel>();
 80                     operationModel model = null;
 81                     DataSet set = GetYSPXRecords(start.ToString(), end.ToString(), classID, opSign);
 82                     for (int i = 0; i < set.Tables[0].Rows.Count; i++)
 83                     {
 84                         model = new operationModel();
 85                         model.OD_ID = int.Parse(set.Tables[0].Rows[i]["od_id"].ToString());
 86                         model.OD_TITLE = set.Tables[0].Rows[i]["od_title"].ToString();
 87                         model._row_number = Convert.ToInt32(set.Tables[0].Rows[i]["Row"].ToString());
 88                         operList.Add(model);
 89                     }
 90                     if (operList.Count > 0)
 91                     {
 92                         Response.Write("{\"Count\":" + recordCount + ",\"operationModel\":" + jss.Serialize(operList) + "}");
 93                     }
 94                     else
 95                     {
 96                         Response.Write("{\"Count\":0,\"operationModel\":null}");
 97                     }
 98                     Response.End();
 99                 }
100             }
101         }
102 
103         /// <summary>
104         /// 从数据库中获取总启用记录的条数
105         /// </summary>
106         /// <returns></returns>
107         private int getAllCount()
108         {
109             return bll.GetRecordCount("question_status=1");
110         }
111 
112 
113         /// <summary>
114         /// 从数据库中获取数据
115         /// </summary>
116         /// <param name="pageIndex">开始</param>
117         /// <param name="pageSize">结束</param>
118         /// <returns>数据集对象</returns>
119         private DataSet GetDataFromDB(int pageIndex, int pageSize)
120         {
121             DataSet set = bll.GetListByPage("", "", pageIndex, pageSize);
122             return set;
123         }

 实现效果:


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Asp.netMvc模块化开发系列(目录)发布时间:2022-07-10
下一篇:
asp.netajax1.0出现&quot;sys&quot;未定义解决方法发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap