您的位置:首页 > 编程语言 > ASP

asp.net 使用ajax分页

2013-03-06 17:35 120 查看
查询出数据,使用sql分页。
public static List<Topic> GetTopicList(int page, int pagesize)
{
string sql = "select * from (select *,row_number() over (order by id) as r_num from Topic) as rum_table where r_num between (@page-1) * @pagesize+1 and @pagesize * @page";
List<Topic> list = new List<Topic>();
using (SqlConnection connection = (SqlConnection)DBHelper.GetDataBase())
{
try
{
SqlCommand cmd = new SqlCommand(sql, connection);
SqlParameter[] parameter = new SqlParameter[]
{
new SqlParameter("@page",page),
new SqlParameter("@pagesize",pagesize)
};
connection.Open();
cmd.Parameters.AddRange(parameter);
SqlDataReader reader = cmd.ExecuteReader();
list = CycleErgodic(reader);
connection.Close();
}
catch (Exception ex)
{
ExceptionManage.WriteLogFile(ex.ToString());
throw new Exception("The system is buys,please try again later...");
}
}
return list;

}
经过BLL层 在 一般处理程序中 输出表格
if (context.Request.Form["getType"].ToString() == "1")
{
if (context.Request.Form["currentPage"] == null || context.Request.Form["pagesize"] == null)
{
context.Response.Write("parameter error");
return;
}
int currentPage = Convert.ToInt32(context.Request.Form["currentPage"]);
int size = Convert.ToInt32(context.Request.Form["pagesize"]);
List<Topic> list = TopicManager.GetTopicList(currentPage,size);
StringBuilder strTable = new StringBuilder();
strTable.Append("<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" style=\"text-align: center; margin: auto\">");
strTable.Append("<tr>");
strTable.Append("<th>主题编号</th><th>标题</th><th>主题内容</th></tr>");
int num = 0;
foreach (Topic topic in list)
{
num++;
string content = topic.Content.Length > 8 ? topic.Content.Substring(0, 8) + "<a onclick=\"queryDetails(" + topic.Id + ")\" href=\"javascript:void(0)\" title='查看详情'>[查看详情]</a>" : topic.Content;
string topicTitle = topic.TopicTitle.Length > 12 ? topic.TopicTitle.Substring(0, 12) + "..." : topic.TopicTitle;
if (num % 2 == 0)
{
strTable.Append(String.Format("<tr onmouseover=\"$(this).css('background-color','#FFFFCC')\" onmouseout=\"$(this).css('background-color','#E5E5E5')\" style=\"background-color: #E5E5E5\"><td>{0}</td><td><a href=\"comment.aspx?id={1}\" title=\"{2}\">{3}</a></td><td>{4}</td></tr>", num, topic.Id, topic.TopicTitle, topicTitle, content));
}
else
{
strTable.Append(String.Format("<tr onmouseover=\"$(this).css('background-color','#FFFFCC')\" onmouseout=\"$(this).css('background-color','#F5F5F5')\" style=\"background-color: #F5F5F5\"><td>{0}</td><td><a href=\"comment.aspx?id={1}\" title=\"{2}\">{3}</a></td><td>{4}</td></tr>", num, topic.Id, topic.TopicTitle, topicTitle, content));
}
}
strTable.Append("</table>");
strTable.Append("<table width=\"80%\"><tr><td align=\"right\"><a id=\"prev\" onclick=\"loadPrev()\" title=\"PageUp\" href=\"javascript:void(0)\">上一页</a> <a id=\"next\" onclick=\"loadNext()\" title=\"PageDown\" href=\"javascript:void(0)\">下一页</a> 第<span id=\"currentPage\"></span>页  共<span id=\"countPage\"></span>页</td></tr></table>");
int count = TopicManager.GetCount();
//将查询数来的总页数存入隐藏域
              strTable.Append(String.Format("<input type=\"hidden\" id=\"count\" value={0} />", count));
context.Response.Write(strTable.ToString());
}


前台 jquery部分

var currentPage = 1;
var pagesize = 20;
var pageCount;
//将当前页和页面大小提交给 一般处理程序 然后将返回的结果显示。
function loadData(currentPage, pagesize) {
$.ajax({
url: "loadTable.ashx",
data: { "getType": "1", "currentPage": currentPage, "pagesize": pagesize, "t": Math.random() },
type: "post",
success: function (data) {
$("div#topicTable").html(data);
var count = $("#count").val();
//算出总页数
if (count % pagesize == 0) {
pageCount = count / pagesize;
} else {
pageCount = parseInt((count / pagesize), 10) + 1;
}
$("#currentPage").html(currentPage);
$("#countPage").html(pageCount);
//当前页==1时,隐藏上一页按钮
if (currentPage == 1) {
$("#prev").hide();
}
//当前页 == 最大页 是,以藏下一页按钮
if (currentPage == pageCount) {
$("#next").hide();
}
}
});
}
//下一页
function loadNext() {
currentPage = currentPage + 1;
loadData(currentPage, pagesize);
}
//上一页
function loadPrev() {
currentPage = currentPage - 1;
loadData(currentPage, pagesize);
}
$(document).ready(function () {
loadData(1,pagesize);
}


<div id="topicTable">
</div>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: