ASP.NET中实现Ajax分页
在页面中指定一个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 + "条记录 共<span id='count'>" + pageCount + "</span>页 ";
htmlStr += "<a href='javascript:void' onclick='GoToPrePage()' id='aPrePage' >前一页</a> ";
htmlStr += "<a href='javascript:void' onclick='GoToNextPage()' id='aNextPage'>后一页</a> ";
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 = "";
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()) : ; //处理接收到的数据
int start = ;
int end = ; if (this._action == "")
{
int recordCount = getAllCount();
int pageCount = (int)Math.Ceiling(((double)recordCount) / ((double)pageSize));
if (pageIndex > pageCount)
{
pageIndex = pageCount;
}
else if (pageIndex < )
pageIndex = ;
start = (pageIndex - ) * pageSize + ;
end = pageIndex * pageSize; IList<Exercise> exerciseLists = new List<Exercise>();
Exercise exercise = null;
DataSet set = GetDataFromDB(start, end);
int id = ;
for (int i = ; i < set.Tables[].Rows.Count; i++)
{
//将第一行记录的ID存入Session
Session["first_id"] = set.Tables[].Rows[]["question_id"];
exercise = new Exercise();
id = Convert.ToInt32(set.Tables[].Rows[i]["question_id"].ToString());
exercise._question_id = id;
exercise._question_content = set.Tables[].Rows[i]["question_content"].ToString();
exercise._question_answer = set.Tables[].Rows[i]["question_answer"].ToString();
exercise._question_analyze = set.Tables[].Rows[i]["question_analyze"].ToString();
exercise._question_status = Convert.ToInt32(set.Tables[].Rows[i]["question_status"].ToString());
exercise._user_id = Convert.ToInt32(set.Tables[].Rows[i]["user_id"].ToString());
exercise._add_time = Convert.ToDateTime(set.Tables[].Rows[i]["add_time"].ToString());
exercise._row_number = Convert.ToInt32(set.Tables[].Rows[i]["Row"].ToString());
exerciseLists.Add(exercise);
}
if (exerciseLists.Count > )
{
Response.Write("{\"Count\":" + recordCount + ",\"Exercise_object\":" + jss.Serialize(exerciseLists) + "}");
}
else
{
Response.Write("{\"Count\":0,\"Exercise_object\":null}");
}
Response.End();
}
else if (this._action == "")
{
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 < )
pageIndex = ;
start = (pageIndex - ) * pageSize + ;
end = pageIndex * pageSize; IList<operationModel> operList = new List<operationModel>();
operationModel model = null;
DataSet set = GetYSPXRecords(start.ToString(), end.ToString(), classID, opSign);
for (int i = ; i < set.Tables[].Rows.Count; i++)
{
model = new operationModel();
model.OD_ID = int.Parse(set.Tables[].Rows[i]["od_id"].ToString());
model.OD_TITLE = set.Tables[].Rows[i]["od_title"].ToString();
model._row_number = Convert.ToInt32(set.Tables[].Rows[i]["Row"].ToString());
operList.Add(model);
}
if (operList.Count > )
{
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;
}
实现效果:

ASP.NET中实现Ajax分页的更多相关文章
- asp.mvc中的vue分页实例,分页组件无法重置reload,解决点击查询按钮后,分页不刷新的问题
刚刚接触Vue.js,现在需要做一个查询功能,并且进行服务端分页.主要思路是在页面中注册一个分页组件,然后进行调用.代码如下 1.引用vue.js,具体去网上下载 2.在html的body中添加如下代 ...
- ASP.NET中无刷新分页
上次介绍了我们代码写的刷新分页,这次就来说说无刷新分页. 这次我们是在上次的基础上改动了一些,我们都知道想要无刷新,就需要Ajax,在我们的ASP.NET中AJax是和一般处理程序配合着用的. 无刷新 ...
- thinkphp中的ajax分页
thinkphp中用ajax分页和普通的ajax分页的区别在于处理位置的不同,thinkphp是在控制器的方法中处理ajax传的值,然后返回数据.下面是一个点击事件触发后,显示的内容用ajax分页. ...
- ASP.Net 中操作Ajax
有时候,越深入去了解一个点,越发觉得自己无知,而之前当自己晓得一两个片面的点还洋洋自得,殊不知,这是多么讽刺,JQuery中有很多优势,比如异步提交值,部分刷新,给用户很好的体验感.目前为止,大部分项 ...
- php--yii框架中的ajax分页与yii框架自带的分页
要想使用Yii分页类 第一步:在控制器层加载分页类 use yii\data\Pagination; 第二步: 使用model层查询数据,并用分分页,限制每页的显示条数 $data = Zhao::f ...
- 在ASP.MVC中使用Ajax
Asp.net MVC 抛弃了Asp.net WebForm那种高度封装的控件,让我们跟底层的HTML有了更多的亲近.可以更自由.更灵活的去控制HTML的结构.样式和行为.Asp.net MVC可以更 ...
- Asp.net中的ajax回调模式(ICallbackEventHandler)
客户端回调本质上就是指通过前端的客户端脚本向服务器端传递相应的数据参数,服务器端再以接受到的参数进行查询和处理,最后将结果回传到客户端进行显示.asp.net 2.0提供了实现无刷新回调的接口ICal ...
- 在asp.net中使用ajax记录
一.问题描述 ajax在mvc中使用频繁,比如cms中的评论功能,但由于涉及到前后端开发,日久容易忘,在此做下记录. 二.内容 控制器中代码示例: /// <summary> /// 在文 ...
- asp.net 中使用 pagedlist 分页并具有查询功能的实现方法
用pagedlist在项目中做分页已N次了,今天再次用实例来实现一个带查询功能的分页例子. 1.在view代码: @using PagedList.Mvc@model BGZS.Models.User ...
随机推荐
- oracle nvl()函数在使用中出现的问题
看一条sql select q.*, r.goods_name from (select nvl(t.goods_code, s.goods_code) goods_code, t.buy_open_ ...
- 提升PHP速度的53个建议
1.如果能将类的方法定义成static,就尽量定义成static,它的速度会提升将近4倍. 2.$row[’id’] 的速度是$row[id]的7倍. 3.echo 比 print 快,并且使用e ...
- jQuery模拟点击A标记
这个问题弄了半小时没想明白,后来觉得是这样的. 菜单 <li class="menu"><a href="xxx.com" target=&q ...
- js学习笔记——数组方法
join() 把数组中所有元素转化为字符串并连接起来,并返回该字符串, var arr=[1,2,3]; var str=arr.join("#"); //str="1# ...
- C#文本文件导入数据库
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windo ...
- uC/OS-II内核架构解析(1)---嵌入式RTOS(转)
uC/OS-II内核架构解析(1)---嵌入式RTOS 1. 嵌入式系统基本模型 2. RTOS设计原则 采用各种算法和策略,始终保持系统行为的可预测性.即在任何情况下,在系统运行的任何时刻,OS的资 ...
- 命令行工具命令 - run包到手机里
命令行工具命令 你完全可以选择不输入以下这些命令,执行这些命令的结果与在 Android Studio 中单击"运行"按钮是一样的. chmod +x gradlew - 此命令只 ...
- LeetCode_Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 多线程操作UI界面的示例 - 更新进度条
http://blog.csdn.net/liang19890820/article/details/52186626
- GDB 的MI接口
背景介绍: libgdb过时了,目前的GDB调试前端都不用libgdb 目前有两种比较流行:- MI接口,现在应该是MI II接口,是Eclipse CDT所采用的方式- emac输出接口,这个似乎有 ...