首页 > 学院 > 开发设计 > 正文

ASP.NET中实现Ajax分页

2019-11-17 01:36:31
字体:
来源:转载
供稿:网友

asp.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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表