C#一個顯示分頁頁碼類

來源:互聯網
上載者:User

在顯示時當前頁碼會自動據中。並可自訂分類連結代碼

using System;

namespace bookshopcn.Service
{
 /// <summary>
 /// Page 的摘要說明。
 /// </summary>
 public class Pager
 {
 public Pager(){}

 protected static int _ButtonCount = 11;
 protected static string _NextPage = "<a href={0}>下一頁</a>";
 protected static string _LinkUrl = "?page={0}";
 protected static string _LastPage = "<a href={0}>上一頁</a>";

 /// <summary>
 /// 下一頁連結
 /// </summary>
 public static string NextPage
 {
  get{return _NextPage;}
  set{_NextPage = value;}
 }

 /// <summary>
 /// 上一頁連結
 /// </summary>
 public static string LastPage
 {
  get{return _LastPage;}
  set{_LastPage = value;}
 }

 /// <summary>
 /// 設定時為格式
 /// </summary>
 public static string NextPageText
 {
  get{return _NextPage;}
  set{_NextPage = value;}
 }

 /// <summary>
 /// 顯示按鈕總數
 /// </summary>
 public static int BottonCount
 {
  get{return _ButtonCount;}
  set{_ButtonCount = value;}
 }
 /// <summary>
 /// 返回頁面的分頁資訊
 /// </summary>
 /// <param name="_RecordCount">記錄總數</param>
 /// <param name="_PageSize">分頁長度</param>
 /// <param name="_PageIndex">當前頁</param>
 /// <returns></returns>
 public static string PageInfo(int _RecordCount,int _PageSize,int _PageIndex,string link)
 { 
  string Firstpage = string.Format("<a href="+link+">[首頁]</a>","1");
  string pageinfo = "共有{0}頁 / 當前第{1}頁 "+Firstpage;
  string pagenext = " <a href="+link+"><b>{0}</b></a> ";
  int PageCount = _RecordCount / _PageSize; // 頁數合計
  PageCount = PageCount <= 0?1:PageCount;
  pageinfo = string.Format(pageinfo,PageCount.ToString(),_PageIndex.ToString());
  string LastPage = string.Format("<a href="+link+">[末頁]</a>",PageCount);

  int n = _ButtonCount/2;  //左右顯示個數
  int StartPage = _PageIndex - n;
  int EndPage = _PageIndex + n;
  _LastPage = string.Format(_LastPage,link);  //上一頁
  _LastPage = _PageIndex-1>1?string.Format(_LastPage,(_PageIndex-1).ToString()):string.Format(_NextPage,"1");

  _NextPage = string.Format(_NextPage,link); //下一頁
  _NextPage = _PageIndex+1<=PageCount?string.Format(_NextPage,_PageIndex.ToString()):string.Format(_NextPage,PageCount.ToString());

  if(EndPage > PageCount )
  {
  StartPage = (_PageIndex - n) - (EndPage-PageCount);
  EndPage = PageCount ;
  }
  if(StartPage < 1 )
  {
  EndPage = _ButtonCount;
  StartPage = 1 ;
  }

  for(int i = StartPage;i<=EndPage;i++)
  {
  if(i != _PageIndex)
   pageinfo += string.Format(pagenext,i);
  else
   pageinfo += " <b>" + i.ToString() + "</b> ";
  }
  pageinfo += LastPage;
  return pageinfo;
 }
 }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.