ASP.NET中实现Ajax分页_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > ASP.NET中实现Ajax分页

ASP.NET中实现Ajax分页

 2014/6/25 17:54:18  望臻风格  程序员俱乐部  我要评论(0)
  • 摘要:在页面中指定一个div容器来接收动态生成的分页数据:1<divid="div_menu">2</div>使用jQuery来请求并处理Json格式数据:1//定义页码与页容量2varpageIndex=1;3varpageSize=15;4varpageCount=0;5varrecordCount=0;6AjaxGetData(pageIndex,pageSize);7//Ajax获取数据8functionAjaxGetData(index,size){9$.ajax(
  • 标签:.net ASP.NET 实现 net Ajax

在页面中指定一个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?id=" + data.Exercise_object[i]._question_id + "' class='cpx12huei' target='content'>";
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         }

 实现效果:

发表评论
用户名: 匿名