[分享]简单无刷新分页

telankes2000 2009-11-29 02:33:01

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>未命名頁面</title>
<style type="text/css">
a:link
{
color: #40ADCE;
text-decoration: none;
}
a:visited
{
text-decoration: none;
}
a:hover
{
text-decoration: underline;
color: Red;
}
a:active
{
text-decoration: none;
}
body
{
margin: 0 auto;
}
</style>

<script type="text/javascript" language="javascript">
var tempBgColor;
var temColor;
var item_id = "";
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
function $(id) { return document.getElementById(id); }
function ChangeBgColor(obj)
{
if (obj.id != item_id)
{
tempBgColor = obj.style.backgroundColor;
temColor = obj.style.color;
obj.style.cursor = "pointer";
obj.style.backgroundColor = "#40ADCE";
}
}
function ClearBgColor(obj)
{
if (obj.id != item_id)
{
obj.style.backgroundColor = tempBgColor;
obj.style.color = temColor;
}
}
function SetBgColor(obj)
{
obj.style.backgroundColor = "#000fff";
obj.style.fontWeight = "bold";
obj.style.color = "#fff";
if (item_id != "")
{
$(item_id).style.backgroundColor = tempBgColor;
$(item_id).style.color = temColor;
$(item_id).style.fontWeight = "";
}
if (item_id == obj.id)
{
item_id = "";
}
else
{
item_id = obj.id;
}
}
function isNumber(obj)
{
var str = /^[0-9]*$/;
if (!str.test(obj.value))
{
window.alert("只能输入数字");
obj.select();
return;
}
}

function GoToPage(obj, CurrentPage, PageCount)
{
// debugger
var GoToPageIndex = null;
if (obj != null)
{
switch (obj.id)
{
case "first":
if (CurrentPage == 1)
return;
CurrentPage = 1;
break;
case "previous":
if (CurrentPage > 1)
CurrentPage = CurrentPage - 1;
else
return;
break;
case "next":
if (CurrentPage == PageCount) return;
if (CurrentPage < PageCount)
{
CurrentPage = CurrentPage + 1;
}

break;
case "last":

if (PageCount != 0)
{
if (CurrentPage == PageCount) return;
CurrentPage = PageCount;
}
break;
case "btnGoToPage":
if ($("txtPageIndex").value == "") return;
CurrentPage = parseInt($("txtPageIndex").value) > parseInt($("btnGoToPage").getAttribute("CommandArgument")) ? parseInt($("btnGoToPage").getAttribute("CommandArgument")) : parseInt($("txtPageIndex").value);
GoToPageIndex = CurrentPage;
break;
default:
//
break;
}
}
var tempGoToPage = GoToPageIndex == null ? "" : "&GoToPageIndex=" + GoToPageIndex + "";
var PageSize = $("txtPageSize") == null ? 10 : $("txtPageSize").value;
var url = "AjaxHandler.ashx?p=" + CurrentPage + "&psize=" + PageSize + "" + tempGoToPage + "&num=" + Math.random();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = function()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.Status == 200)
{
if (xmlHttp.responseText != "")
{
$("showmsg").innerHTML = xmlHttp.responseText;
$("lblmsg").innerHTML = "";
}
}
else
{
$("lblmsg").innerHTML = xmlHttp.statusText;
return;
}
}
else
{
$("lblmsg").innerHTML = "数据正在加载中...";
}
}
xmlHttp.send(null);
}
window.onload = function()
{
GoToPage(null, 1, 0);
}

</script>

</head>
<body>
<span id="lblmsg" style="color: Red; font-size: 9pt; position: absolute; z-index: 1">
</span>
<div id="showmsg">
</div>
</body>
</html>



...全文
315 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxhysoftsodc 2009-11-29
  • 打赏
  • 举报
回复
收藏 支持
wuyq11 2009-11-29
  • 打赏
  • 举报
回复
支持
claymore1114 2009-11-29
  • 打赏
  • 举报
回复
学习 谢谢分享
telankes2000 2009-11-29
  • 打赏
  • 举报
回复
数据库用到是Sqlserver2005
用到存储过程

CREATE PROCEDURE [dbo].[SYS_PageData]
(
@SqlCommandText nvarchar(4000),
@CurrentPage int, --頁次
@PageSize int , --行數
@PageCount int out,--总页数
@TotalRecord int out --总记录数
)
AS
set nocount on
DECLARE
@P1 int, --P1是游標的id
@rowcount int

SET @SqlCommandText=replace(replace(replace(replace(replace(replace(replace(replace(@SqlCommandText,'exec ',''),'insert ',''),'delete ',''),'update ',''),'drop ',''),'alter ',''),'chr(',''),'mid(','')

exec sp_cursoropen @P1 output ,@SqlCommandText,@scrollopt=1,@ccopt=1,@rowcount=@TotalRecord output

-- 頁數 行數
set @PageCount = ceiling(1.0*@TotalRecord/@PageSize)
set @CurrentPage=(@CurrentPage-1)*@PageSize+1

exec sp_cursorfetch @P1,16,@CurrentPage,@PageSize

exec sp_cursorclose @P1

set nocount off
telankes2000 2009-11-29
  • 打赏
  • 举报
回复

<%@ WebHandler Language="C#" Class="AjaxHandler" %>

using System;
using System.Web;
using System.Text;
using System.Data;
public class AjaxHandler : IHttpHandler
{
StringBuilder sb = new StringBuilder();
private DataSet GetPageData(int CurrentPage, int PageSize, string SqlWhere, out int PageCount, out int ToatlRecord)
{
Database db = new Database();
string sql = @"SELECT CustomerKey, GeographyKey, CustomerAlternateKey, FirstName, BirthDate, Gender, EmailAddress, Phone FROM DimCustomer {0}";
sql = string.Format(sql, SqlWhere); ;
db.AddInParameter("@SqlCommandText", sql, SqlDbType.NVarChar);
db.AddInParameter("@CurrentPage", CurrentPage, SqlDbType.Int);
db.AddInParameter("@PageSize", PageSize, SqlDbType.Int);
db.AddOutParameter("@PageCount", SqlDbType.Int);
db.AddOutParameter("@TotalRecord", SqlDbType.Int);
DataSet ds = db.ExecuteDataSet(CommandType.StoredProcedure, "SYS_PageData");
PageCount = Convert.ToInt32(db.GetParameterValue("@PageCount"));
ToatlRecord = Convert.ToInt32(db.GetParameterValue("@TotalRecord"));
return ds;
}
public bool IsReusable
{
get
{
return false;
}
}

public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["p"] == null || context.Request.QueryString["psize"] == null)
return;
int CurrentPage = context.Request.QueryString["p"] == null ? 1 : Convert.ToInt32(context.Request.QueryString["p"]);
int PageSize = context.Request.QueryString["psize"] == null ? 10 : Convert.ToInt32(context.Request.QueryString["psize"]);
string GoToPageIndex = context.Request.QueryString["GoToPageIndex"] == null ? "" : Convert.ToString(context.Request.QueryString["GoToPageIndex"]);
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.ContentEncoding = Encoding.UTF8;
int PageCount, TotalRecord;
DataSet ds = GetPageData(CurrentPage, PageSize, "", out PageCount, out TotalRecord);
if (ds.Tables[1].Columns.Contains("rowstat"))
ds.Tables[1].Columns.Remove("rowstat");
SetSelectPageStyle(CurrentPage, PageCount, TotalRecord, PageSize, 2, GoToPageIndex);
SetReturnData(ds.Tables[1]);
context.Response.Write(sb.ToString());
sb = null;
context.Response.End();
}

void CreateTurnPageScript()
{
sb.Append("<script language=\"javascript\" type=\"text/javascript\" src=\"AjaxHandler.js\">");
// sb.AppendFormat("window.alert({0})", PageIndex);
sb.Append("</script>");

}

void CreateSelectPageStyle(int CurrentPage, int PageCount, int PageStyleType)
{
switch (PageStyleType)
{
case 1:
this.SetSelectPageButtonStyle(CurrentPage, PageCount);
break;
case 2:
this.SetSelectPageNumberStyle(CurrentPage, PageCount);
break;
default:
this.SetSelectPageNormalStyle(CurrentPage, PageCount);
break;
}
}
/// <summary>
/// 设置分页的样式
/// </summary>
/// <param name="CurrentPage">当前页</param>
/// <param name="PageCount">总页数</param>
/// <param name="TotalRecord">总记录数</param>
/// <param name="PageSize">每页显示的数量</param>
void SetSelectPageStyle(int CurrentPage, int PageCount, int TotalRecord, int PageSize, int PageStyleType, string GoToPageIndex)
{

sb.Append("<div style='font-size:10pt;color:#40ADCE;text-align:center' id='selectpage'>");
CreateSelectPageStyle(CurrentPage, PageCount, PageStyleType);
sb.AppendFormat("转到第<input type=\"text\" id=\"txtPageIndex\" style=\"width:30px;height:15px\" value=\"{0}\" onkeyup=\"isNumber(this)\">页<input type=\"button\" CommandArgument=\"{1}\" style=\"height:20px;margin:0px;\" value=\"GO\" id=\"btnGoToPage\" onclick=\"GoToPage(this,0,0)\">", GoToPageIndex, PageCount);
sb.AppendFormat("当前第<font color='red'>{0}</font>页 共<font color='red'>{1}</font>页 共有<font color='red'>{2}</font>条数据 每页<input type='text' id=\"txtPageSize\" value=\"{3}\" style=\"width:30px;height:15px\" onkeyup=\"isNumber(this)\">条数据 ", CurrentPage, PageCount, TotalRecord, PageSize);
sb.Append("</div>");
}
void SetSelectPageNumberStyle(int CurrentPage, int PageCount)
{
int step = 5;//步长
int StartPage = 1;
int EndPage = CurrentPage + step;

if (CurrentPage > step)//如果当前页大于步长
{
StartPage = CurrentPage - step;//开始页
}
if (EndPage > PageCount)
{
EndPage = PageCount;
}
else
{
EndPage = EndPage - 1;
}
if (CurrentPage > StartPage)
{
sb.AppendFormat("<a href=\"javascript:\" id=\"first\" onclick=\"GoToPage(this,{0},{1})\" style=\"margin-right:7px;\" title=\"首页\">首页</a>", CurrentPage, PageCount);
sb.AppendFormat("<a href=\"javascript:\" id=\"previous\" onclick=\"GoToPage(this,{0},{1})\" style=\"margin-right:7px;\" title=\"上一页\">上一页</a>", CurrentPage, PageCount);
}
for (int i = StartPage; i <= EndPage; i++)
{
if (CurrentPage == i)//如果是当前页
{
sb.AppendFormat("<span style='background-color:#40ADCE;color:#fff;border:solid 1px #40ADCE;margin-right:7px;padding:1px'> {0} </span>", i);
}
else
{
sb.AppendFormat("<a href=\"javascript:\" id=\"num_{0}\" onclick=\"GoToPage(this,{0},{1})\" style='margin-right:7px;border:solid 1px #40ADCE;padding:1px;' title=\"第{0}页\"> {0} </a>", i, PageCount);
}
}
if (CurrentPage < EndPage)
{
sb.AppendFormat("<a href=\"javascript:\" id=\"next\" onclick=\"GoToPage(this,{0},{1})\" style=\"margin-right:7px;\" title=\"下一页\">下一页</a>", CurrentPage, PageCount);
sb.AppendFormat("<a href=\"javascript:\" id=\"last\" onclick=\"GoToPage(this,{0},{1})\" title=\"尾页\" style=\"margin-right:7px;\">尾页</a>", CurrentPage, PageCount);
}

}
void SetSelectPageNormalStyle(int CurrentPage, int PageCount)
{
sb.AppendFormat("<a href=\"javascript:\" id=\"first\" onclick=\"GoToPage(this,{0},{1})\" >首页</a> ", CurrentPage, PageCount);
sb.AppendFormat("<a href=\"javascript:\" id=\"previous\" onclick=\"GoToPage(this,{0},{1})\" >上一页</a> ", CurrentPage, PageCount);
sb.AppendFormat("<a href=\"javascript:\" id=\"next\" onclick=\"GoToPage(this,{0},{1})\" >下一页</a> ", CurrentPage, PageCount);
sb.AppendFormat("<a href=\"javascript:\" id=\"last\" onclick=\"GoToPage(this,{0},{1})\" >尾页</a> ", CurrentPage, PageCount);
}
void SetSelectPageButtonStyle(int CurrentPage, int PageCount)
{
sb.AppendFormat("<input type=\"button\" id=\"first\" onclick=\"GoToPage(this,{0},{1})\" value=\"首页\" />", CurrentPage, PageCount);
sb.AppendFormat("<input type=\"button\" id=\"previous\" onclick=\"GoToPage(this,{0},{1})\" value=\"上一页\" />", CurrentPage, PageCount);
sb.AppendFormat("<input type=\"button\" id=\"next\" onclick=\"GoToPage(this,{0},{1})\" value=\"下一页\" />", CurrentPage, PageCount);
sb.AppendFormat("<input type=\"button\" id=\"last\" onclick=\"GoToPage(this,{0},{1})\" value=\"尾页\" />", CurrentPage, PageCount);
}
void CreateColumns(DataColumnCollection columns)
{
sb.Append("<tr style=\"color:#fff;background-color:#006699;font-weight:bold;font-size:10pt\">");
for (int i = 0; i < columns.Count; i++)
{
sb.Append("<td>");
sb.AppendFormat("{0}", columns[i].ColumnName);
sb.Append("</td>");
}
sb.Append("</tr>");
}
void SetRowValue(DataRow row)
{
for (int i = 0; i < row.Table.Columns.Count; i++)
{
sb.Append("<td>");
sb.AppendFormat("{0}", row[row.Table.Columns[i].ColumnName].ToString());
sb.Append("</td>");
}
}
void SetReturnData(DataTable dt)
{
sb.Append("<table id=\"table1\" border=\"1\" style=\"BORDER-collapse:collapse;width:100%\" cellSpacing=\"0\" cellPadding=\"0\" >");
CreateColumns(dt.Columns);
int i = 0;//onmouseover=\"ChangeBgColor(this)\" onmouseout=\"ClearBgColor(this)\"
foreach (DataRow row in dt.Rows)
{
sb.Append("<tr id=\"tr_" + i + "\" style=\"font-size:9pt\" onmouseover=\"ChangeBgColor(this)\" onmouseout=\"ClearBgColor(this)\" onclick=\"SetBgColor(this)\">");
SetRowValue(row);
sb.Append("</tr>");
i++;
}
sb.Append("</table>");

}
}

62,050

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧