当前位置: 编程技术>jquery
jquery.pagination与JSON实现动态无刷新分页的代码
来源: 互联网 发布时间:2014-09-03
本文导语: 本节内容: jquery.pagination、JSON 动态无刷新分页 1,aspx页面部分 代码示例: jquery.pagination +JSON 动态无刷新分页 - www. NewsID Title SmallClassName Author UpdateTime ...
本节内容:
jquery.pagination、JSON 动态无刷新分页
1,aspx页面部分
代码示例:
jquery.pagination +JSON 动态无刷新分页 - www.
NewsID
Title
SmallClassName
Author
UpdateTime
var pageIndex =0; //页索引
var pageSize =20; //每页显示的条数
$(function() {
Init(0);
$("#Pagination").pagination(, {
callback: PageCallback,
prev_text: '上一页',
next_text: '下一页',
items_per_page: pageSize,
num_display_entries: 5,
current_page: pageIndex,
num_edge_entries: 1
});
function PageCallback(index, jq) {
Init(index);
}
});
function Init(pageIndex) {
$.ajax({
type: "POST",
dataType: "text",
url: 'SqlPage.aspx',
data: "pageIndex=" + (pageIndex + 1) + "&pageSize=" + pageSize,
success: function(data) {
if(data!=""){
$("#tblData tr:gt(0)").remove();//移除所有的数据行
data=$.parseJSON(data);
$.each(data.News,function(index,news){
$("#tblData").append(""+news.NewsID+""+news.Title+""+news.SmallClassName+""+news.Author+""+news.Updatetime+""); //将返回的数据追加到表格
});
}
}
});
}
2,cs代码部分:
代码示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class SqlPage : System.Web.UI.Page
{
public int pageCount = 0;
public static string connString = "server=192.168.1.91;database=ReportDB;uid=sa;pwd=123456";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pageCount = GetTotalPage();
if (Request["pageIndex"] != null && Request["pageSize"] != null)
{
int pageSize = Convert.ToInt32(Request["pageSize"]) == 0 ? 1 : Convert.ToInt32(Request["pageSize"]);
int pageIndex = Convert.ToInt32(Request["pageIndex"]) == 0 ? 1 : Convert.ToInt32(Request["pageIndex"]);
Response.Write(GetOnePage(pageSize, pageIndex));
Response.End();
}
}
}
public int GetTotalPage()
{
DBHelper.connString = connString;
string sql = "select count(*) from News";
int rs = Convert.ToInt32(DBHelper.ExecuteScalar(sql));
return rs;
}
public string GetOnePage(int pageSize, int pageIndex)
{
DBHelper.connString = connString;
string sql = string.Empty;
sql = "SELECT TOP " + pageSize + " NewsID,Title,SmallClassName,Author,Updatetime FROM News WHERE NewsID NOT IN (SELECT TOP " + pageSize * (pageIndex - 1) + " NewsID FROM News ORDER BY NewsID DESC) ORDER BY NewsID DESC";
DataTable dt = DBHelper.QueryBySql(sql);
return ConvertJson.ToJson(dt, "News");
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class SqlPage : System.Web.UI.Page
{
public int pageCount = 0;
public static string connString = "server=192.168.1.91;database=ReportDB;uid=sa;pwd=123456";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
pageCount = GetTotalPage();
if (Request["pageIndex"] != null && Request["pageSize"] != null)
{
int pageSize = Convert.ToInt32(Request["pageSize"]) == 0 ? 1 : Convert.ToInt32(Request["pageSize"]);
int pageIndex = Convert.ToInt32(Request["pageIndex"]) == 0 ? 1 : Convert.ToInt32(Request["pageIndex"]);
Response.Write(GetOnePage(pageSize, pageIndex));
Response.End();
}
}
}
public int GetTotalPage()
{
DBHelper.connString = connString;
string sql = "select count(*) from News";
int rs = Convert.ToInt32(DBHelper.ExecuteScalar(sql));
return rs;
}
public string GetOnePage(int pageSize, int pageIndex)
{
DBHelper.connString = connString;
string sql = string.Empty;
sql = "SELECT TOP " + pageSize + " NewsID,Title,SmallClassName,Author,Updatetime FROM News WHERE NewsID NOT IN (SELECT TOP " + pageSize * (pageIndex - 1) + " NewsID FROM News ORDER BY NewsID DESC) ORDER BY NewsID DESC";
DataTable dt = DBHelper.QueryBySql(sql);
return ConvertJson.ToJson(dt, "News");
}
}