asp.net 通用分頁顯示輔助類(改進版)

來源:互聯網
上載者:User

閑暇時重新再看之前很多項目代碼的時候,發現很多總是有那麼點缺陷的代碼,如芒刺入骨,令人心煩。掙紮良久,苦口婆心勸薦自己今後要爭取把某些代碼寫的更易用更靈活更完善一些。比如在這篇已經提及到的通用用戶端分頁顯示輔助類(AspNetPager),在大大小小項目中出現頻率非常高,但是顯然對分頁形式的選擇和樣式的控制弱了點。雖然之前這個功能的實現都是按照實際項目的需求來實現的,對舊項目沒有影響,但是這也不能成為該功能沒有充分實現擴充性和通用性的理由,這點向來自傲的樓豬也不得不承認。

1、分頁模式和樣式擴充
(1)定義枚舉 複製代碼 代碼如下:/// <summary>
/// 表格對齊枚舉
/// </summary>
public enum TbAlignEnum
{
Left = 1,

Center = 2,

Right = 3
}

/// <summary>
/// 分頁模式
/// </summary>
public enum PagerModoule
{
/// <summary>
/// 普通分頁模式
/// </summary>
Normal = 1,

/// <summary>
/// 統計分頁模式
/// </summary>
Statistics = 2
}

(2)根據枚舉呈現 複製代碼 代碼如下:/// <summary>
/// 分頁實用類
/// </summary>
public sealed class AspNetPager
{
#region common

private const string defaultAlign = "center";//預設對齊
private const string leftAlign = "left";
private const string rightAlign = "right";

/// <summary>
/// 擷取頁數
/// </summary>
/// <param name="objs">總記錄數</param>
/// <param name="recordCountPerPage">每頁記錄數</param>
/// <returns></returns>
public static int GetPageCout(object[] objs, int recordCountPerPage)
{
return (int)Math.Ceiling(((double)objs.Length / (double)recordCountPerPage));
}

/// <summary>
/// 擷取頁數
/// </summary>
/// <param name="totalCount">總記錄數</param>
/// <param name="recordCountPerPage">每頁記錄數</param>
/// <returns></returns>
public static int GetPageCout(int totalCount, int recordCountPerPage)
{
int result = 0;
if (totalCount % recordCountPerPage == 0)
{
result = totalCount / recordCountPerPage;
}
else
{
result = totalCount / recordCountPerPage + 1;
}
return result;
}

#endregion

#region render pager

/// <summary>
/// 寫分頁頁碼(沒有表格)
/// </summary>
/// <param name="pagerMode"></param>
/// <param name="response"></param>
/// <param name="baseString"></param>
/// <param name="totalCount">總記錄數</param>
/// <param name="nowPage">當前頁數</param>
/// <param name="recordCountPerPage">每頁記錄數</param>
public static void RenderPager(PagerModoule pagerMode, HttpResponse response, int totalCount, int nowPage, int recordCountPerPage, string baseString)
{
int pageCount = GetPageCout(totalCount, recordCountPerPage);
string pagerString = string.Empty;
if (pageCount > 0)
{
switch (pagerMode)
{
case PagerModoule.Normal:
pagerString = CreateLinkUrl(baseString, pageCount, nowPage, recordCountPerPage);
break;
case PagerModoule.Statistics:
pagerString = CreateStatisticLinkUrl(baseString, totalCount, pageCount, nowPage, recordCountPerPage);
break;
default:
pagerString = CreateLinkUrl(baseString, pageCount, nowPage, recordCountPerPage);
break;
}
response.Write(pagerString);
}
}

/// <summary>
/// 寫分頁頁碼(有表格)
/// </summary>
/// <param name="pagerMode"></param>
/// <param name="alignEnum"></param>
/// <param name="response"></param>
/// <param name="baseString"></param>
/// <param name="totalCount">總記錄數</param>
/// <param name="nowPage">當前頁數</param>
/// <param name="recordCountPerPage">每頁記錄數</param>
public static void RenderTablePager(PagerModoule pagerMode, TbAlignEnum alignEnum, HttpResponse response, int totalCount, int nowPage, int recordCountPerPage, string baseString)
{
int pageCount = GetPageCout(totalCount, recordCountPerPage);
if (pageCount > 0)
{
string align = string.Empty;
switch (alignEnum)
{
case TbAlignEnum.Left:
align = leftAlign;
break;
case TbAlignEnum.Center:
align = defaultAlign;
break;
case TbAlignEnum.Right:
align = rightAlign;
break;
default:
align = defaultAlign;
break;
}
StringBuilder sbTable = new StringBuilder();
sbTable.AppendFormat("<table><tr align='{0}'><td>", align);
string pagerString = string.Empty;
switch (pagerMode)
{
case PagerModoule.Normal:
pagerString = CreateLinkUrl(baseString, pageCount, nowPage, recordCountPerPage);
break;
case PagerModoule.Statistics:
pagerString = CreateStatisticLinkUrl(baseString, totalCount, pageCount, nowPage, recordCountPerPage);
break;
default:
pagerString = CreateLinkUrl(baseString, pageCount, nowPage, recordCountPerPage);
break;
}
sbTable.Append(pagerString);
sbTable.Append("</td></tr></table>");
response.Write(sbTable.ToString());
}
}

#endregion

#region create link

/// <summary>
/// 產生分頁字串(顯示頁數和每頁記錄數相關)
/// </summary>
/// <param name="baseString"></param>
/// <param name="pageCount">頁數</param>
/// <param name="nowPage">當前頁數</param>
/// <param name="recordCountPerPage">每頁記錄數(推薦記錄數:10)</param>
/// <returns></returns>
private static string CreateLinkUrl(string baseString, int pageCount, int nowPage, int recordCountPerPage)
{
StringBuilder sb = new StringBuilder();
int from, to;
if (nowPage - recordCountPerPage > 0)
{
from = nowPage - recordCountPerPage;
}
else
from = 1;
if (pageCount == 0)
pageCount = 1;
if (pageCount - nowPage - recordCountPerPage > 0)
{
to = nowPage + recordCountPerPage;
}
else
to = pageCount;

if (baseString.IndexOf("?") == -1)
baseString += "?";
else
baseString += "&";
sb.Append(string.Format("<a href=\"{0}pageIndex=1\" >首頁</a>", baseString));
if (pageCount > 1 && nowPage > 1)
{
sb.AppendFormat("<a href=\"{0}pageIndex={1}\" >上一頁</a>", baseString, (nowPage - 1).ToString());
}
else
{
sb.Append("<a href='javascript:void(0);' style='color:gray;' >上一頁</a>");
}
for (int i = from; i <= to; i++)
{
if (i == nowPage)
{
sb.AppendFormat(" <a href='javascript:void(0);' style='color:red;' >{0}</a>", nowPage.ToString());
}
else
{
sb.AppendFormat(" <a href=\"{0}pageIndex={1}\" >{1}</a>", baseString, i);
}
}
if (pageCount > 1 && nowPage < pageCount)
{
sb.AppendFormat("<a href=\"{0}pageIndex={1}\" >下一頁</a>", baseString, (nowPage + 1).ToString());
}
else
{
sb.Append("<a href=\"javascript:void(0);\" style='color:gray;' >下一頁</a>");
}
sb.Append(string.Format(" <a href={0}pageIndex={1} >尾頁</a>", baseString, pageCount));
return sb.ToString();
}

/// <summary>
/// 產生含統計資訊的分頁字串(顯示頁數和每頁記錄數相關)
/// </summary>
/// <param name="baseString"></param>
/// <param name="totalCount">總記錄數</param>
/// <param name="pageCount">頁數</param>
/// <param name="nowPage">當前頁數</param>
/// <param name="recordCountPerPage">每頁記錄數(推薦記錄數:10)</param>
/// <returns></returns>
private static string CreateStatisticLinkUrl(string baseString, int totalCount, int pageCount, int nowPage, int recordCountPerPage)
{
StringBuilder sb = new StringBuilder();
string numricPager = CreateLinkUrl(baseString, pageCount, nowPage, recordCountPerPage);//普通數字分頁
sb.AppendFormat("總共<span style='color:red;'>{0}</span>條記錄,共<span style='color:red;'>{1}</span>頁,當前第<span style='color:red;'>{2}</span>頁 ",
totalCount, pageCount, nowPage);
sb.Append(numricPager);
return sb.ToString();
}

#endregion
}

ps1:除了表格內容對齊外,代碼裡對樣式的控制幾乎都是寫入程式碼,個人認為編碼階段對樣式的控制越少越好。理想狀態下,樣式應該讓UI全部在外部控制,而不應該由程式員來設計實現,這裡結合實際情況取折中方案。
ps2:分頁顯示的方式其實還可以擴充,具體請參考gridview控制項分頁模式。
2、頁面內調用 複製代碼 代碼如下:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Pager.aspx.cs" Inherits="WebTest.Pager" %>

<%@ Import Namespace=" DotNet.Common.WebForm" %>
<!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>aspnet簡單分頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%DotNet.Common.WebForm.AspNetPager.RenderTablePager(PagerModoule.Normal, TbAlignEnum.Right, Response, 93, 10, 10, "Pager.aspx"); %>
</div>
</form>
</body>
</html>

3、有圖有真相
(1)、普通模式


(2)、帶統計模式

最後,歡迎補充,期待您的建議和意見。

示範檔案打包下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.