大学IT网 - 最懂大学生的IT学习网站! QQ资料交流群:367606806
当前位置:大学IT网 > ASP.NET技巧 > ASP.NET中实现Ajax分页

ASP.NET中实现Ajax分页

关键词:Ajax分页ASP.NET  阅读(547) 赞(20)

[摘要]本文是对ASP.NET中实现Ajax分页的讲解,对学习ASP.NET编程技术有所帮助,与大家分享。

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

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

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

//定义页码与页容量
         var pageIndex = 1;
         var pageSize = 15;
         var pageCount = 0;
         var recordCount = 0;
         AjaxGetData(pageIndex, pageSize);
         //Ajax获取数据
         function AjaxGetData(index, size) {
             $.ajax({
                 url: "ProcessData.aspx",
                 type: "Get",
                 data: "pageindex=" + index + "&pagesize=" + size + "&rnd=" + new Date(),
                 dataType: "json",
                 success: function (data) {
                     var htmlStr = "";
                     htmlStr += "<table width=100%>";
                     for (var i = 0; i < data.Exercise_object.length; i++) {
                         htmlStr += "<tr><td  class='rr' onmouseover='javascript:onOver(this)' onmouseout='javascript:onOut(this)'onclick='javascript:onDown(this);'>";
                         htmlStr += "<a href='voucher/Exercise_Detail.aspx?id=" + data.Exercise_object[i]._question_id + "' class='cpx12huei' target='content'>";
                         htmlStr += "第" + data.Exercise_object[i]._row_number + "题";
                         htmlStr += "</a>";
                         htmlStr += "</td></tr>";
                     }
                     htmlStr += "<tr style='text-align:center;'>";
                     htmlStr += "<td>";
                     recordCount = Number(data.Count);
                     pageCount = Math.ceil(recordCount / pageSize);
                     htmlStr += "共" + recordCount + "条记录&nbsp;&nbsp;共<span id='count'>" + pageCount + "</span>页&nbsp;&nbsp;&nbsp;&nbsp;";
                     htmlStr += "<a href='javascript:void' onclick='GoToPrePage()' id='aPrePage' >前一页</a>&nbsp;&nbsp; ";
                     htmlStr += "<a href='javascript:void' onclick='GoToNextPage()' id='aNextPage'>后一页</a>&nbsp;&nbsp; ";
                     htmlStr += "</td>";
                     htmlStr += "</tr>";
                     htmlStr += "</table>";
                     $("#div_menu").html(htmlStr);
                 },
                 error: function (XMLHttpRequest, textStatus, errorThrown) {
                     alert(XMLHttpRequest);
                     alert(textStatus);
                     alert(errorThrown);
                 }
             });
         }
         //前一页
         function GoToPrePage() {
             pageIndex -= 1;
             if (pageIndex < 1) {
                 pageIndex = 1;
                 return;
             }
             AjaxGetData(pageIndex, pageSize);
         }
         //后一页
         function GoToNextPage() {
             pageIndex += 1;
             if (pageIndex > pageCount) {
                 pageIndex = pageCount;
                 return;
             }
             AjaxGetData(pageIndex, pageSize);
         } 

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

private readonly BLL.D_Accounting_Entry_Exercise bll = new BLL.D_Accounting_Entry_Exercise();
         private string _action = "0";
         protected void Page_Load(object sender, EventArgs e)
         {
             Int32 pageIndex = Int32.MinValue;
             Int32 pageSize = Int32.MinValue;
 
             if (Request["action"] != null)
                 this._action = Request["action"];
 
             JavaScriptSerializer jss = new JavaScriptSerializer();
             if (Request["pageindex"] != null)
             {
                 pageIndex = Int32.Parse(Request["pageindex"].ToString());
                 pageSize = Request["pagesize"] != null ? Int32.Parse(Request["pagesize"].ToString()) : 10;
 
                 //处理接收到的数据
                 int start = 0;
                 int end = 0;
 
                 if (this._action == "0")
                 {
                     int recordCount = getAllCount();
                     int pageCount = (int)Math.Ceiling(((double)recordCount) / ((double)pageSize));
                     if (pageIndex > pageCount)
                     {
                         pageIndex = pageCount;
                     }
                     else if (pageIndex < 1)
                         pageIndex = 1;
                     start = (pageIndex - 1) * pageSize + 1;
                     end = pageIndex * pageSize;
 
                     IList<Exercise> exerciseLists = new List<Exercise>();
                     Exercise exercise = null;
                     DataSet set = GetDataFromDB(start, end);
                     int id = 0;
                     for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                     {
                         //将第一行记录的ID存入Session
                         Session["first_id"] = set.Tables[0].Rows[0]["question_id"];
                         exercise = new Exercise();
                         id = Convert.ToInt32(set.Tables[0].Rows[i]["question_id"].ToString());
                         exercise._question_id = id;
                         exercise._question_content = set.Tables[0].Rows[i]["question_content"].ToString();
                         exercise._question_answer = set.Tables[0].Rows[i]["question_answer"].ToString();
                         exercise._question_analyze = set.Tables[0].Rows[i]["question_analyze"].ToString();
                         exercise._question_status = Convert.ToInt32(set.Tables[0].Rows[i]["question_status"].ToString());
                         exercise._user_id = Convert.ToInt32(set.Tables[0].Rows[i]["user_id"].ToString());
                         exercise._add_time = Convert.ToDateTime(set.Tables[0].Rows[i]["add_time"].ToString());
                         exercise._row_number = Convert.ToInt32(set.Tables[0].Rows[i]["Row"].ToString());
                         exerciseLists.Add(exercise);
                     }
                     if (exerciseLists.Count > 0)
                     {
                         Response.Write("{\"Count\":" + recordCount + ",\"Exercise_object\":" + jss.Serialize(exerciseLists) + "}");
                     }
                     else
                     {
                         Response.Write("{\"Count\":0,\"Exercise_object\":null}");
                     }
                     Response.End();
                 }
                 else if (this._action == "1")
                 {
                     string classID = Request["classid"];
                     string opSign = Request["opsign"];
                     int recordCount = GetYSPXCount(opSign, classID);
                     int pageCount = (int)Math.Ceiling(((double)recordCount) / ((double)pageSize));
                     if (pageIndex > pageCount)
                     {
                         pageIndex = pageCount;
                     }
                     else if (pageIndex < 1)
                         pageIndex = 1;
                     start = (pageIndex - 1) * pageSize + 1;
                     end = pageIndex * pageSize;
 
                     IList<operationModel> operList = new List<operationModel>();
                     operationModel model = null;
                     DataSet set = GetYSPXRecords(start.ToString(), end.ToString(), classID, opSign);
                     for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                     {
                         model = new operationModel();
                         model.OD_ID = int.Parse(set.Tables[0].Rows[i]["od_id"].ToString());
                         model.OD_TITLE = set.Tables[0].Rows[i]["od_title"].ToString();
                         model._row_number = Convert.ToInt32(set.Tables[0].Rows[i]["Row"].ToString());
                         operList.Add(model);
                     }
                     if (operList.Count > 0)
                     {
                         Response.Write("{\"Count\":" + recordCount + ",\"operationModel\":" + jss.Serialize(operList) + "}");
                     }
                     else
                     {
                         Response.Write("{\"Count\":0,\"operationModel\":null}");
                     }
                     Response.End();
                 }
             }
         }
 
         /// <summary>
         /// 从数据库中获取总启用记录的条数
         /// </summary>
         /// <returns></returns>
         private int getAllCount()
         {
             return bll.GetRecordCount("question_status=1");
         }
 
 
         /// <summary>
         /// 从数据库中获取数据
         /// </summary>
         /// <param name="pageIndex">开始</param>
         /// <param name="pageSize">结束</param>
         /// <returns>数据集对象</returns>
         private DataSet GetDataFromDB(int pageIndex, int pageSize)
         {
             DataSet set = bll.GetListByPage("", "", pageIndex, pageSize);
             return set;
         }

实现效果:



相关评论