程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> asp.net 通用分頁顯示輔助類(改進版)

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

編輯:ASP.NET基礎
閒暇時重新再看之前很多項目代碼的時候,發現很多總是有那麼點缺陷的代碼,如芒刺入骨,令人心煩。掙扎良久,苦口婆心勸薦自己今後要爭取把某些代碼寫的更易用更靈活更完善一些。比如在這篇已經提及到的通用客戶端分頁顯示輔助類(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)、帶統計模式

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

演示文件打包下載

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved