简单的数字分页代码
分页说来简单但也不易,网上分页控件数不胜数,苦于找不到适合自己用的只好自己做一个,不是说成型的控件不好,只是觉得功能太强大,而且调用过程实在复杂。
要实现复杂功能的朋友不必往下再看,本程序仅实现了简单分页、可以数字显示(可固定显示页数)、数据源为datatable,仅此而已。话不多数上代码。
aspx
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="adks.Web.Default" %>
<!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="myRep" runat="server" OnItemDataBound="myRep_ItemDataBound">
<ItemTemplate>
<li>
<%#Eval("mycol") %>
</li>
</ItemTemplate>
<FooterTemplate>
<asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink>
<asp:PlaceHolder ID="myPh" runat="server"></asp:PlaceHolder>
<asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="hlla" runat="server" Text="末页"></asp:HyperLink>
<br />
共<asp:Label ID="lbl_Count" runat="server" Text=""></asp:Label> 条记录
<asp:Label ID="lbl_PageCount" runat="server" Text=""></asp:Label>条记录/页
共<asp:Label ID="lbl_all" runat="server" Text=""></asp:Label>页
当前为第<asp:Label ID="lbl_cur" runat="server" Text=""></asp:Label>页
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="adks.Web.Default" %>
<!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="myRep" runat="server" OnItemDataBound="myRep_ItemDataBound">
<ItemTemplate>
<li>
<%#Eval("mycol") %>
</li>
</ItemTemplate>
<FooterTemplate>
<asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink>
<asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink>
<asp:PlaceHolder ID="myPh" runat="server"></asp:PlaceHolder>
<asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink>
<asp:HyperLink ID="hlla" runat="server" Text="末页"></asp:HyperLink>
<br />
共<asp:Label ID="lbl_Count" runat="server" Text=""></asp:Label> 条记录
<asp:Label ID="lbl_PageCount" runat="server" Text=""></asp:Label>条记录/页
共<asp:Label ID="lbl_all" runat="server" Text=""></asp:Label>页
当前为第<asp:Label ID="lbl_cur" runat="server" Text=""></asp:Label>页
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace adks.Web
{
public partial class Default : System.Web.UI.Page
{
private int Count = 0;
private int pageCount = 20; //显示数
private string pagename = "Default.aspx?"; //跳转页
protected void Page_Load(object sender, EventArgs e)
{
myRep.DataSource = SetPds();
myRep.DataBind();
}
public PagedDataSource SetPds()
{
DataTable myTable = new DataTable();
myTable.Columns.Add("mycol", typeof(string));
for (int i = 0; i < 1000; i++)
{
DataRow myr = myTable.NewRow();
myr["mycol"] = "gudao " + i.ToString() + " haha";
myTable.Rows.Add(myr);
}
PagedDataSource myPds = new PagedDataSource();
myPds.AllowPaging = true;
myPds.DataSource = myTable.DefaultView;
myPds.PageSize = pageCount;
Count = myTable.Rows.Count;
if (Request.QueryString["p"] != null)
myPds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["p"]);
else
myPds.CurrentPageIndex = 0;
return myPds;
}
protected void myRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
int currentPage = SetPds().CurrentPageIndex; //当前页号
int pageCount = SetPds().PageCount; //分页数量
int startPage, endPage; //开始分页号 结束分页号
int proPageCount = 5; //当前页前显示数
int nextPageCount = 4;
Label lbl_c = (Label)e.Item.FindControl("lbl_cur");
lbl_c.Text = Convert.ToString(currentPage + 1);
Label lbl_a = (Label)e.Item.FindControl("lbl_all");
lbl_a.Text = Convert.ToString(pageCount);
HyperLink hl_fir = (HyperLink)e.Item.FindControl("hlfir");
hl_fir.NavigateUrl = pagename + "p=0";
HyperLink hl_p = (HyperLink)e.Item.FindControl("hlp");
HyperLink hl_n = (HyperLink)e.Item.FindControl("hln");
HyperLink hl_la = (HyperLink)e.Item.FindControl("hlla");
hl_la.NavigateUrl = pagename + "p=" + Convert.ToString(pageCount - 1);
//数据量
Label lbl_count = (Label)e.Item.FindControl("lbl_Count");
lbl_count.Text = Convert.ToString(this.Count);
//每页显示数据量
Label lbl_PageCount = (Label)e.Item.FindControl("lbl_PageCount");
lbl_PageCount.Text = Convert.ToString(this.pageCount);
if (currentPage <= 0)
{
hl_fir.Enabled = false;
hl_p.Enabled = false;
hl_n.Enabled = true;
hl_la.Enabled = true;
}
else
{
hl_p.NavigateUrl = pagename + "p=" + Convert.ToString(currentPage - 1);
}
if (currentPage > pageCount - 2)
{
hl_fir.Enabled = true;
hl_p.Enabled = true;
hl_n.Enabled = false;
hl_la.Enabled = false;
if (currentPage == 0)
{
hl_fir.Enabled = true;
hl_p.Enabled = false;
hl_n.Enabled = false;
hl_la.Enabled = false;
}
}
else
{
hl_n.NavigateUrl = pagename + "p=" + Convert.ToString(currentPage + 1);
}
//动态生成数字页码
//获得第一个页号
if (currentPage <= proPageCount)
startPage = 0;
else
{
int linkLength = (pageCount - currentPage + 1) +proPageCount;
startPage = currentPage - proPageCount;
while (linkLength < proPageCount + nextPageCount + 1 && startPage > 1)
{
linkLength++;
startPage--;
}
}
//获得最后一个页号
if (currentPage + nextPageCount >= pageCount)
endPage = pageCount;
else
{
int linkLength = (currentPage - startPage + 1) + nextPageCount;
endPage = currentPage + nextPageCount;
while (linkLength < proPageCount + nextPageCount + 1 && endPage < pageCount)
{
linkLength++;
endPage++;
}
}
//循环生成页号
for (int i = startPage; i < endPage; i++)
{
Label lb1 = new Label();
lb1.Text = "[";
Label lb2 = new Label();
lb2.Text = "]";
HyperLink myLkb = new HyperLink();
myLkb.ID = ((int)(i + 1)).ToString();
myLkb.NavigateUrl = pagename + "p=" + i;
myLkb.Text = ((int)(i + 1)).ToString();
((PlaceHolder)e.Item.FindControl("myPh")).Controls.Add(lb1);
((PlaceHolder)e.Item.FindControl("myPh")).Controls.Add(myLkb);
((PlaceHolder)e.Item.FindControl("myPh")).Controls.Add(lb2);
}
}
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace adks.Web
{
public partial class Default : System.Web.UI.Page
{
private int Count = 0;
private int pageCount = 20; //显示数
private string pagename = "Default.aspx?"; //跳转页
protected void Page_Load(object sender, EventArgs e)
{
myRep.DataSource = SetPds();
myRep.DataBind();
}
public PagedDataSource SetPds()
{
DataTable myTable = new DataTable();
myTable.Columns.Add("mycol", typeof(string));
for (int i = 0; i < 1000; i++)
{
DataRow myr = myTable.NewRow();
myr["mycol"] = "gudao " + i.ToString() + " haha";
myTable.Rows.Add(myr);
}
PagedDataSource myPds = new PagedDataSource();
myPds.AllowPaging = true;
myPds.DataSource = myTable.DefaultView;
myPds.PageSize = pageCount;
Count = myTable.Rows.Count;
if (Request.QueryString["p"] != null)
myPds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["p"]);
else
myPds.CurrentPageIndex = 0;
return myPds;
}
protected void myRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
int currentPage = SetPds().CurrentPageIndex; //当前页号
int pageCount = SetPds().PageCount; //分页数量
int startPage, endPage; //开始分页号 结束分页号
int proPageCount = 5; //当前页前显示数
int nextPageCount = 4;
Label lbl_c = (Label)e.Item.FindControl("lbl_cur");
lbl_c.Text = Convert.ToString(currentPage + 1);
Label lbl_a = (Label)e.Item.FindControl("lbl_all");
lbl_a.Text = Convert.ToString(pageCount);
HyperLink hl_fir = (HyperLink)e.Item.FindControl("hlfir");
hl_fir.NavigateUrl = pagename + "p=0";
HyperLink hl_p = (HyperLink)e.Item.FindControl("hlp");
HyperLink hl_n = (HyperLink)e.Item.FindControl("hln");
HyperLink hl_la = (HyperLink)e.Item.FindControl("hlla");
hl_la.NavigateUrl = pagename + "p=" + Convert.ToString(pageCount - 1);
//数据量
Label lbl_count = (Label)e.Item.FindControl("lbl_Count");
lbl_count.Text = Convert.ToString(this.Count);
//每页显示数据量
Label lbl_PageCount = (Label)e.Item.FindControl("lbl_PageCount");
lbl_PageCount.Text = Convert.ToString(this.pageCount);
if (currentPage <= 0)
{
hl_fir.Enabled = false;
hl_p.Enabled = false;
hl_n.Enabled = true;
hl_la.Enabled = true;
}
else
{
hl_p.NavigateUrl = pagename + "p=" + Convert.ToString(currentPage - 1);
}
if (currentPage > pageCount - 2)
{
hl_fir.Enabled = true;
hl_p.Enabled = true;
hl_n.Enabled = false;
hl_la.Enabled = false;
if (currentPage == 0)
{
hl_fir.Enabled = true;
hl_p.Enabled = false;
hl_n.Enabled = false;
hl_la.Enabled = false;
}
}
else
{
hl_n.NavigateUrl = pagename + "p=" + Convert.ToString(currentPage + 1);
}
//动态生成数字页码
//获得第一个页号
if (currentPage <= proPageCount)
startPage = 0;
else
{
int linkLength = (pageCount - currentPage + 1) +proPageCount;
startPage = currentPage - proPageCount;
while (linkLength < proPageCount + nextPageCount + 1 && startPage > 1)
{
linkLength++;
startPage--;
}
}
//获得最后一个页号
if (currentPage + nextPageCount >= pageCount)
endPage = pageCount;
else
{
int linkLength = (currentPage - startPage + 1) + nextPageCount;
endPage = currentPage + nextPageCount;
while (linkLength < proPageCount + nextPageCount + 1 && endPage < pageCount)
{
linkLength++;
endPage++;
}
}
//循环生成页号
for (int i = startPage; i < endPage; i++)
{
Label lb1 = new Label();
lb1.Text = "[";
Label lb2 = new Label();
lb2.Text = "]";
HyperLink myLkb = new HyperLink();
myLkb.ID = ((int)(i + 1)).ToString();
myLkb.NavigateUrl = pagename + "p=" + i;
myLkb.Text = ((int)(i + 1)).ToString();
((PlaceHolder)e.Item.FindControl("myPh")).Controls.Add(lb1);
((PlaceHolder)e.Item.FindControl("myPh")).Controls.Add(myLkb);
((PlaceHolder)e.Item.FindControl("myPh")).Controls.Add(lb2);
}
}
}
}
}
如果有需要传递其它参数的请将pagename修改并加上自己的参数重新构造。如果数据量特别大建议不要使用。