在页面中指定一个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;
}
实现效果: