我這裡封裝在用戶控件裡。你也可以封裝在類文件中。自己定義:
[csharp]
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 HHWeb.UserControl
{
public partial class Paging : System.Web.UI.UserControl
{
public StringBuilder PagingControlInfo = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
GetPaging();
}
void GetPaging()
{
//判斷如果當前頁為負數或者零時,NowPage=1
if (NowPage <= 0)
NowPage = 1;
//判斷如果當前頁大於總頁數時,NowPage=PageCount
int PageCount = GetPageCount();
if (NowPage > PageCount)
NowPage = PageCount;
//首頁和上一頁()
if (NowPage != 1)
{
PagingControlInfo.Append("<a href=\""+PageName+"?NowPage=1\">首頁</a>");
PagingControlInfo.Append("<a href=\""+PageName+"?NowPage=" + (NowPage - 1) + "\">上一頁</a>");
}
//如果分頁總頁數小於頁碼數(PageNumber)時,不顯示省略號(...)
if (PageCount <= PageNumber)
{
for (int i = 1; i <= PageCount; i++)
GetPageNumberStr(i);
}
else
{
if (PageNumber % 2 != 0)
{
LeftPageNumberOne = (PageNumber / 2) + 1;
LeftPageNumberTwo = (PageNumber / 2);
RightPageNumber = (PageNumber / 2);
}
else
{
LeftPageNumberOne = (PageNumber / 2) + 1;
LeftPageNumberTwo = (PageNumber / 2);
RightPageNumber = (PageNumber / 2) - 1;
}
//總頁數大於頁碼數(PageNumber)時(簡單處理--算法)
if (NowPage > LeftPageNumberOne && NowPage < PageCount - RightPageNumber)
{
PagingControlInfo.Append("<span>...</span>");
for (int i = NowPage - LeftPageNumberTwo; i <= NowPage + RightPageNumber; i++)
GetPageNumberStr(i);
PagingControlInfo.Append("<span>...</span>");
}
else if (NowPage >= PageCount - RightPageNumber)
{
PagingControlInfo.Append("<span>...</span>");
for (int i = (PageCount - PageNumber) + 1; i <= PageCount; i++)
GetPageNumberStr(i);
}
else
{
for (int i = 1; i <= PageNumber; i++)
GetPageNumberStr(i);
PagingControlInfo.Append("<span>...</span>");
}
}
//下一頁和末頁
if (NowPage != PageCount)
{
PagingControlInfo.Append("<a href=\""+PageName+"?NowPage=" + (NowPage + 1) + "\">下一頁</a>");
PagingControlInfo.Append("<a href=\""+PageName+"?NowPage=" + PageCount + "\">末頁</a>");
}
}
#region //拼接頁碼方法
public void GetPageNumberStr(int i)
{
if (i == NowPage)
PagingControlInfo.Append("<a href=\""+PageName+"?NowPage="+i+"\" style=\"border:0px;color:#ff5a00 ;\">" + i + "</a>");
else
PagingControlInfo.Append("<a href=\""+PageName+"?NowPage=" + i + "\">" + i + "</a>");
}
#endregion
#region //計算總頁數
public int GetPageCount()
{
if (PageSize == 0)
PageSize = 20;//每頁顯示的記錄條數為"0",則默認為"20"
if (Totalcount % PageSize == 0)
return (Totalcount / PageSize);
else
return (Totalcount / PageSize) + 1;
}
#endregion
#region //分頁必須提供的基本屬性
/// <summary>
/// 當前頁
/// </summary>
public int NowPage { get; set; }
/// <summary>
/// 每頁顯示的記錄條數
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 總條數
/// </summary>
public int Totalcount { get; set; }
/// <summary>
/// 頁碼(意思是前台分頁控件顯示的頁數)
/// </summary>
public int PageNumber { get; set; }
/// <summary>
/// 當前頁面名稱如:"index.aspx"
/// </summary>
public string PageName { get;set;}
#endregion
#region //分頁基本屬性
/// <summary>
/// 左頁碼屬性一
/// </summary>
public int LeftPageNumberOne { get; set; }
/// <summary>
/// 左頁碼屬性二
/// </summary>
public int LeftPageNumberTwo { get; set; }
/// <summary>
/// 右頁碼
/// </summary>
public int RightPageNumber { get; set; }
#endregion
}
}
效果如下:自己給一下分頁樣式就Ok了。www.2cto.com
作者:yfz19890410