- 效果图:
- Paging.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Paging.ascx.cs" Inherits="Paging" %>
<script type="text/javascript">
function newPage(type) {
var targetPage = $(".txtTargetPage").val();
if (type == "target" && targetPage == "")
{
alert('请输入页码!');
return false;
} else if(type == "target" && targetPage != ""){
var maxPage = $(".lblTotalPage").html();
if (parseInt(targetPage) > parseInt(maxPage)) {
alert("输入的页码不能大于当前最大页码!");
$(".txtTargetPage").val('');
return false;
}
} else
$(".txtTargetPage").val('');
$('.txtGuideTag').val(type);
$(".btnFy").click();
}
function DigitInput(obj, event) {
event = window.event || event;
if (event.keyCode == 37 | event.keyCode == 39) {
return;
}
obj.value = obj.value.replace(/\D/g, "");
if (parseInt(obj.value) == 0) obj.value = "";
}
</script>
<table style="width: 100%; margin-left: 40px">
<tr style="height: 25px; vertical-align: middle;">
<td style="width: 60%">共检索到:<asp:Label ID="lblTotalCount" runat="server" Text="0"></asp:Label>条!<asp:TextBox ID="txtGuideTag" class="txtGuideTag"
runat="server" Style="display: none"></asp:TextBox>每页<select id="pSize" style=""
runat="server">
<option value="5">5</option>
<option selected value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
</select>条
</td>
<td style="text-align: right; vertical-align: middle">
<span id="pre" runat="server">[<a href="#" onclick="newPage('-1')" style='color: blue; text-decoration: underline'>前页</a>]
</span>
<span id="pre2" runat="server" style="display: none;">
<span style='color: blue'>[<a href="#" style='color: blue; text-decoration: none'>前页</a>]</span>
</span>
</td>
<td style="text-align: left; vertical-align: middle">
<span id="next" runat="server">[<a href="#" onclick="newPage('+1')" style='color: blue; text-decoration: underline'>后页</a>]</span>
<span id="next2" runat="server" style="display: none;">
<span style='color: blue'>[<a href="#" style='color: blue; text-decoration: none'>后页</a>]</span></span>
</td>
<td style="vertical-align: middle">页码:<span id="lblCurr" runat="server">1</span>/<span id="lblTotalPage" class="lblTotalPage"
runat="server"></span>
跳转到<input id="txtTargetPage" class="txtTargetPage" runat="server" style="width: 20px;"
onkeyup="DigitInput(this,event);" onpaste="return false" />页[<a href="#" onclick="newPage('target')"
style='color: blue; text-decoration: underline'>转到</a>]
</td>
</tr>
</table>
<asp:Button ID="btnFy" runat="server" class="btnFy" Text="分页" OnClick="btnFy_Click" Style="display: none" />
<asp:Button ID="btnSearch" runat="server" class="btnSearch" Text="查询" OnClick="btnSearch_Click" Style="display: none" />
using System;
using System.Web.UI;
public partial class Paging : System.Web.UI.UserControl
{
#region 属性
public int TotalCount { get; set; }
private int startIndex;
public int StartIndex
{
get
{
return startIndex;
}
set
{
startIndex = value;
}
}
private int endIndex;
public int EndIndex
{
get
{
return endIndex;
}
set
{
endIndex = value;
}
}
#endregion
int pageSize = 0;
int pageCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
pageSize = int.Parse(pSize.Value);
lblTotalCount.Text = TotalCount.ToString();
pageCount = (TotalCount % pageSize == 0) ? (TotalCount / pageSize) : (TotalCount / pageSize + 1);
lblTotalPage.InnerText = pageCount.ToString();
if (!IsPostBack)
InintControl();
}
protected void btnFy_Click(object sender, EventArgs e)
{
InintControl();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
int currPage = 1;
startIndex = pageSize * (currPage - 1) + 1;
endIndex = pageSize * currPage;
lblCurr.InnerText = currPage.ToString();
GuidStyle(currPage, pageCount);
}
#region 控件显示
protected void InintControl()
{
#region 分页控件初始化
int pageCount = (TotalCount % pageSize == 0) ? (TotalCount / pageSize) : (TotalCount / pageSize + 1);
int currPage = int.Parse(lblCurr.InnerText);
string page = this.txtGuideTag.Text;
this.txtGuideTag.Text = "";
if (page == "target")
{
currPage = int.Parse(this.txtTargetPage.Value.Trim());
}
else
{
switch (page)
{
case "+1":
currPage = currPage + 1;
break;
case "-1":
currPage = currPage - 1;
break;
}
}
if (currPage > pageCount)
{
currPage = 1;
}
lblCurr.InnerText = currPage.ToString();
startIndex = pageSize * (currPage - 1) + 1;
endIndex = pageSize * currPage;
#endregion
GuidStyle(currPage, pageCount);
}
protected void GuidStyle(int currPage, int pageCount)
{
#region “上一页/下一页”样式
if (currPage == 1)
{
pre.Attributes["style"] = "display:none";
pre2.Attributes["style"] = "display:block";
}
else
{
pre.Attributes["style"] = "display:block";
pre2.Attributes["style"] = "display:none";
}
if ((currPage == pageCount) || (currPage == 1 && pageCount == 0))
{
next.Attributes["style"] = "display:none";
next2.Attributes["style"] = "display:block";
}
else
{
next.Attributes["style"] = "display:block";
next2.Attributes["style"] = "display:none";
}
#endregion
}
#endregion
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ Register Src="~/Paging.ascx" TagPrefix="ZDControl" TagName="Paging" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js"></script>
<title></title>
<script>
$(function () {
$('#Search').click(function () {
$('.btnSearch').click();
})
})
</script>
</head>
<body>
<form id="form1" runat="server">
年份:<input type="text" id="txtAssYesr" value="" runat="server" />
<input type="button" id="Search" value="查询" />
<div id="div1" runat="server"></div>
<ZDControl:Paging runat="server" ID="MyPaging" />
</form>
</body>
</html>
-----------------------------------------
private void Page_Load(object sender, System.EventArgs e)
{
MyPaging.TotalCount = GetTotalCount();
}
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
int start = MyPaging.StartIndex, end = MyPaging.EndIndex;
string where = GetWhere();
ShowTable(where,start, end);
}
protected string GetWhere()
{
string year = txtYear.Value.Trim();
if (year == "")
return " 1=1 ";
else
return " year=" +txtYear.Value.Trim();
}
public int GetTotalCount(string whereSql)
{
string sql = " SELECT count(id) FROM 表 where "+ GetWhere();
return int.Parse(DbHelperSQL.ExecuteScalar(CommandType.Text, sql));
}
public void ShowTable(string whereSql,int start,int end)
{
string sql = @" SELECT * FROM ( SELECT row_No = ROW_NUMBER() OVER ( ORDER BY ID ASC )
,id,assessName FROM 表
) tbl WHERE row_No BETWEEN "+start+" AND "+end +" and "+whereSql;
DataTable dt2 = DbHelperSQL.Query(sql).Tables[0];
div1.InnerHtml= DataTableToHtml(dt2);
}
- aspx中使用分页控件:AspNetPager.dll
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
<style type="text/css">
.paginator { font: 12px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;}
.paginator a {border:solid 1px #ccc;color:#0063dc;cursor:pointer;text-decoration:none;}
.paginator a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}
.paginator .cpb {border:1px solid #F50;font-weight:700;color:#F50;background-color:#ffeee5;}
.paginator a:hover {border:solid 1px #F50;color:#f60;text-decoration:none;}
.paginator a,.paginator a:visited,.paginator .cpb,.paginator a:hover
{float:left;height:16px;line-height:16px;min-width:10px;_width:10px;margin-right:5px;text-align:center;
white-space:nowrap;font-size:12px;font-family:Arial,SimSun;padding:0 3px;}
</style>
<webdiyer:AspNetPager ID="aspPage" runat="server" FirstPageText="首页"
AlwaysShow="true" Width="100%"
LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页"
onpagechanged="aspPage_PageChanged" CssClass="paginator"
CurrentPageButtonClass="cpb"
CustomInfoHTML="共%PageCount%页,当前为第%CurrentPageIndex%页,每页%PageSize%条" >
</webdiyer:AspNetPager>
protected void aspPage_PageChanged(object sender, EventArgs e)
{
InitData();
}
protected void InitData()
{
aspPage.PageSize = 20;
int recount = 0;
string strWhere="";
DataSet ds = ExtendTool.ExecuteSearch(out recount, "PR_SAP_SendLeve", "ID", aspPage.CurrentPageIndex, aspPage.PageSize, "", "ID", strWhere);
aspPage.RecordCount = recount;
}