常用分页代码备份
2010-10-11 14:31
239 查看
在公司里开发一些页面经常要坐分页,做了一个通用点的,备份一下,以后只要copy了
.cs端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace WebFormTest
{
public partial class WebForm1 : System.Web.UI.Page
{
public int PageIndex = 1;
private int pageSize = 12;
private int totalCount = 0;
const int PageShowNum = 5;//导航条显示半数
public string CurrentUrl = "WebForm1.aspx";
private int totalPage
{
get
{
return (totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1);
}
}
/// <summary>
/// 检查参数
/// </summary>
private void CheckParam()
{
if (!int.TryParse(Request.QueryString["pageIndex"], out PageIndex))
PageIndex = 1;
}
protected void Page_Load(object sender, EventArgs e)
{
this.CheckParam();
if (!Page.IsPostBack)
{
this.BindData();
}
}
/// <summary>
/// 绑定展示数据
/// </summary>
private void BindData()
{
List<int> list = GetData();//获取数据
this.totalCount = list.Count;
PagedDataSource datasource = new PagedDataSource();
datasource.AllowPaging = true;
datasource.DataSource = list;
datasource.PageSize = pageSize;
datasource.CurrentPageIndex = PageIndex - 1;
rpt.DataSource = datasource;
rpt.DataBind();
this.SetPager(PageIndex, totalPage);
}
/// <summary>
/// 用来获取数据
/// </summary>
/// <returns></returns>
private List<int> GetData()
{
List<int> list = new List<int>();
for (int index = 1; index < 1000; index++)
{
list.Add(index);
}
return list;
}
/// <summary>
/// 设置分页
/// </summary>
/// <param name="currentNum">当前页,也就是PageIndex</param>
/// <param name="totalNum">总页数</param>
private void SetPager(int currentNum, int totalNum)
{
int _beginNum = 1, _endNum = 1;
_beginNum = PageShowNum < currentNum ? currentNum - PageShowNum : 1;
if (PageShowNum + currentNum >= totalNum)
{
_endNum = totalNum;
}
else
{
if (int.Equals(_beginNum, 1))
{
_endNum = 2 * PageShowNum + _beginNum;
}
else
{
_endNum = PageShowNum + currentNum;
}
}
StringBuilder sb = new StringBuilder(" ");
if (currentNum > 1)
{
sb.AppendFormat(" <a class='f_a' href='{0}'> {1} </a> ", CurrentUrl, "首页");
}
else if (currentNum == 1)
{
sb.Append(" <a class='f_a' > 首页 </a> ");
}
string _className = string.Empty;
while (_beginNum <= _endNum)
{
if (_beginNum == PageIndex)
{
_className = "f_s";
sb.AppendFormat(" <a class='{0}' style='color:red' > {1} </a> ", PageIndex, _beginNum);
}
else
{
_className = "f_a";
sb.AppendFormat(" <a href='{0}?pageIndex={1}'> {1} </a> ", CurrentUrl, _beginNum);
}
_beginNum++;
}
if (currentNum < totalNum)
{
sb.AppendFormat(" <a href='{0}?pageIndex={1}'> {2} </a> ",CurrentUrl,totalNum,"尾页");
}
else if (currentNum == totalNum)
{
sb.Append(" <a class='f_a' > 尾页 </a> ");
}
sb.Append("<input type='text' id='txtPageIndex' style='width:40px;font-size:12px' onKeyPress='return Page.GoEnter(event)' /> 页 <input type='button' onclick='return Page.GoPage()' value='GO' />");
sb.AppendFormat(" {0}/{1} 页 共 {2} 篇", currentNum, totalNum, totalCount);
ltrPage.Text = sb.ToString();
ClientScript.RegisterClientScriptBlock(Page.GetType(), "ChannelArray", "var totalPage=" + totalNum + ";\r\n", true);
}
}
}
.aspx页面代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormTest.WebForm1" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<%# Container.DataItem.ToString() %>
<br />
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Literal ID="ltrPage" runat="server" />
</div>
<script type="text/javascript">
(function (win) {
if (!win.Page) {
var Page = {
GoPage: function () {
var page = document.getElementById("txtPageIndex").value;
if (page.length === 0) {
alert("请您输入页码");
return false;
};
var IsNum = function () {
var strNum = '1234567890';
var isNum = true;
for (var i = 0; i < page.length; i += 1) {
var c = page.charAt(i);
if (strNum.indexOf(c) < 0) {
isNum = false;
break;
};
};
return isNum;
}
if (IsNum()) {
if (parseInt(page) > totalPage && parseInt(page) > 1) {
alert("您输入的页数超过总页数,请重新输入!");
return false;
};
var currentUrl = "<%=CurrentUrl %>" + "?pageIndex=" + page;
location.href = currentUrl;
return false;
} else {
alert("您输入的信息有误,请重新输入!");
}
},
GoEnter: function (evt) {
evt = evt || window.event;
if (evt && evt.keyCode == 13)
return Page.GoPage();
}
}
win.Page = Page;
}
document.getElementById("txtPageIndex").value = "<%=PageIndex %>";
})(window);
</script>
</form>
</body>
</html>
.cs端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace WebFormTest
{
public partial class WebForm1 : System.Web.UI.Page
{
public int PageIndex = 1;
private int pageSize = 12;
private int totalCount = 0;
const int PageShowNum = 5;//导航条显示半数
public string CurrentUrl = "WebForm1.aspx";
private int totalPage
{
get
{
return (totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1);
}
}
/// <summary>
/// 检查参数
/// </summary>
private void CheckParam()
{
if (!int.TryParse(Request.QueryString["pageIndex"], out PageIndex))
PageIndex = 1;
}
protected void Page_Load(object sender, EventArgs e)
{
this.CheckParam();
if (!Page.IsPostBack)
{
this.BindData();
}
}
/// <summary>
/// 绑定展示数据
/// </summary>
private void BindData()
{
List<int> list = GetData();//获取数据
this.totalCount = list.Count;
PagedDataSource datasource = new PagedDataSource();
datasource.AllowPaging = true;
datasource.DataSource = list;
datasource.PageSize = pageSize;
datasource.CurrentPageIndex = PageIndex - 1;
rpt.DataSource = datasource;
rpt.DataBind();
this.SetPager(PageIndex, totalPage);
}
/// <summary>
/// 用来获取数据
/// </summary>
/// <returns></returns>
private List<int> GetData()
{
List<int> list = new List<int>();
for (int index = 1; index < 1000; index++)
{
list.Add(index);
}
return list;
}
/// <summary>
/// 设置分页
/// </summary>
/// <param name="currentNum">当前页,也就是PageIndex</param>
/// <param name="totalNum">总页数</param>
private void SetPager(int currentNum, int totalNum)
{
int _beginNum = 1, _endNum = 1;
_beginNum = PageShowNum < currentNum ? currentNum - PageShowNum : 1;
if (PageShowNum + currentNum >= totalNum)
{
_endNum = totalNum;
}
else
{
if (int.Equals(_beginNum, 1))
{
_endNum = 2 * PageShowNum + _beginNum;
}
else
{
_endNum = PageShowNum + currentNum;
}
}
StringBuilder sb = new StringBuilder(" ");
if (currentNum > 1)
{
sb.AppendFormat(" <a class='f_a' href='{0}'> {1} </a> ", CurrentUrl, "首页");
}
else if (currentNum == 1)
{
sb.Append(" <a class='f_a' > 首页 </a> ");
}
string _className = string.Empty;
while (_beginNum <= _endNum)
{
if (_beginNum == PageIndex)
{
_className = "f_s";
sb.AppendFormat(" <a class='{0}' style='color:red' > {1} </a> ", PageIndex, _beginNum);
}
else
{
_className = "f_a";
sb.AppendFormat(" <a href='{0}?pageIndex={1}'> {1} </a> ", CurrentUrl, _beginNum);
}
_beginNum++;
}
if (currentNum < totalNum)
{
sb.AppendFormat(" <a href='{0}?pageIndex={1}'> {2} </a> ",CurrentUrl,totalNum,"尾页");
}
else if (currentNum == totalNum)
{
sb.Append(" <a class='f_a' > 尾页 </a> ");
}
sb.Append("<input type='text' id='txtPageIndex' style='width:40px;font-size:12px' onKeyPress='return Page.GoEnter(event)' /> 页 <input type='button' onclick='return Page.GoPage()' value='GO' />");
sb.AppendFormat(" {0}/{1} 页 共 {2} 篇", currentNum, totalNum, totalCount);
ltrPage.Text = sb.ToString();
ClientScript.RegisterClientScriptBlock(Page.GetType(), "ChannelArray", "var totalPage=" + totalNum + ";\r\n", true);
}
}
}
.aspx页面代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormTest.WebForm1" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<%# Container.DataItem.ToString() %>
<br />
</ItemTemplate>
</asp:Repeater>
<br />
<asp:Literal ID="ltrPage" runat="server" />
</div>
<script type="text/javascript">
(function (win) {
if (!win.Page) {
var Page = {
GoPage: function () {
var page = document.getElementById("txtPageIndex").value;
if (page.length === 0) {
alert("请您输入页码");
return false;
};
var IsNum = function () {
var strNum = '1234567890';
var isNum = true;
for (var i = 0; i < page.length; i += 1) {
var c = page.charAt(i);
if (strNum.indexOf(c) < 0) {
isNum = false;
break;
};
};
return isNum;
}
if (IsNum()) {
if (parseInt(page) > totalPage && parseInt(page) > 1) {
alert("您输入的页数超过总页数,请重新输入!");
return false;
};
var currentUrl = "<%=CurrentUrl %>" + "?pageIndex=" + page;
location.href = currentUrl;
return false;
} else {
alert("您输入的信息有误,请重新输入!");
}
},
GoEnter: function (evt) {
evt = evt || window.event;
if (evt && evt.keyCode == 13)
return Page.GoPage();
}
}
win.Page = Page;
}
document.getElementById("txtPageIndex").value = "<%=PageIndex %>";
})(window);
</script>
</form>
</body>
</html>