public enum DataShowControl { DataList, GridView, Repeater };
public enum FenYeStyle { Stander, ListStyle, Google };
public class CommFenYe
{
public CommFenYe()
{}
/// <summary>
/// 分頁方法
/// </summary>
/// <param name="ds">數(shù)據(jù)源</param>
/// <param name="pageSize">每頁顯示數(shù)</param>
/// <param name="gv">數(shù)據(jù)控件</param>
/// <param name="control">數(shù)據(jù)控件樣式</param>
/// <param name="fystyle">分頁樣式</param>
/// <returns>返回分頁樣式字符串</returns>
public string fenye(DataSet ds, int pageSize, Object gv, DataShowControl control, FenYeStyle fystyle)
{
PagedDataSource pagesource = new PagedDataSource(); //實例化分頁數(shù)據(jù)源
pagesource.AllowPaging = true;//分頁屬性
pagesource.PageSize = pageSize;//每頁顯示數(shù)
pagesource.DataSource = ds.Tables[0].DefaultView;//提供數(shù)據(jù)源
int currentPage = 1;
if (!string.IsNullOrEmpty(HttpContext.Current.Request["page"]))
{
currentPage = Convert.ToInt16(HttpContext.Current.Request["page"]);
if (currentPage < 1) currentPage = 1;
if (currentPage > pagesource.PageCount) currentPage = pagesource.PageCount;
}
pagesource.CurrentPageIndex = currentPage - 1;
if (control == DataShowControl.DataList) //給不同的數(shù)據(jù)控件綁定數(shù)據(jù)
{
((DataList)gv).DataSource = pagesource;
((DataList)gv).DataBind();
}
if (control == DataShowControl.GridView)
{
((GridView)gv).DataSource = pagesource;
((GridView)gv).DataBind();
}
if (control == DataShowControl.Repeater)
{
((Repeater)gv).DataSource = pagesource;
((Repeater)gv).DataBind();
}
string str = "", path = HttpContext.Current.Request.Path;//用不同的方法實現(xiàn)分頁
if (fystyle == FenYeStyle.Stander)//標(biāo)準(zhǔn)分頁
{
str = "<a href=" + path + "?page=1>首頁</a>|" +
"<a href=" + path + "?page=" + (currentPage - 1) + ">上一頁</a>|" +
"<a href=" + path + "?page=" + (currentPage + 1) + ">下一頁</a>|" +
"<a href=" + path + "?page=" + pagesource.PageCount + ">末頁</a>";
}
if (fystyle == FenYeStyle.ListStyle)//列表分頁
{
for (int i = 1; i <= pagesource.PageCount; i++)
{
if (i == currentPage)
{
str += "[<font color='red' size='4'><b>" + i + "</b></font>] ";
}
else
{
str += "[<a href='" + path + "?page=" + i + "'>" + i + "</a>] ";
}
}
}
int startcount = 0, endcount = 0;
int page = currentPage;
int allpage = pagesource.PageCount;
string query_string = path;
int next = currentPage + 1, pre = currentPage - 1;
if (fystyle == FenYeStyle.Google)//狗狗分頁
{
startcount = (page + 5) > allpage ? allpage - 9 : page - 4;//中間頁起始序號
endcount = page < 5 ? 10 : page + 5;//中間頁終止序號
//為了避免輸出的時候產(chǎn)生負(fù)數(shù),設(shè)置如果小于1就從序號1開始
if (startcount < 1) { startcount = 1; }
//頁碼+5的可能性就會產(chǎn)生最終輸出序號大于總頁碼,那么就要將其控制在頁碼數(shù)之內(nèi)
if (allpage < endcount) { endcount = allpage; }
str = "共<font color='red'>" + allpage + "</font>頁/當(dāng)前第<font color='red'>" + page + "</font>頁";
str += page > 1 ? "<a href=\"" + query_string + "?page=1\">首頁</a> <a href=\"" + query_string + "?page=" + pre + "\">上一頁</a>" : "首頁 上一頁";
//中間頁處理,這個增加時間復(fù)雜度,減小空間復(fù)雜度
for (int i = startcount; i <= endcount; i++)
{
str += page == i ? " <font color=\"#ff0000\">" + i + "</font>" : " <a href=\"" + query_string + "?page=" + i + "\">" + i + "</a>";
}
str += page != allpage ? " <a href=\"" + query_string + "?page=" + next + "\">下一頁</a><a href=\"" + query_string + "?page=" + allpage + "\">末頁</a>" : " 下一頁 末頁";
}
return str;//返回分頁樣式字符串
}
}