C#分頁 訪google

wl1121發表於2009-09-04

using System;  
using System.Data;  
using System.Configuration;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Text;  
  
/// <summary>  
/// Pagenumber 的摘要說明  
/// </summary>  
public static class Pagenumber  
{  
    ///   <summary>   
         ///  建立並顯示分頁器  
         ///   </summary>   
    public static string BuildPager(int totalRecords, int currentPage, int pageSize, int pid)  
    {  
        int alter = 4;  
        int startPage = 1;  
        int endPage = currentPage + alter;  
        int totalPages = CalculateTotalPages(totalRecords, pageSize);  
  
        if (currentPage > alter)  
        {  
            startPage = currentPage - alter;  
        }  
  
        if (endPage > totalPages)  
        {  
            endPage = totalPages;  
        }  
  
        string strTemp = @" <a href='Default.aspx?p={0}&pno={1}'>{2}</a>&nbsp; ";  
        StringBuilder sb = new StringBuilder("");  
        if (currentPage != startPage)  
        {  
            sb.Append(string.Format(strTemp, currentPage - 1, currentPage - 1, " 上一頁 "));  
        }  
  
        for (int i = startPage; i <= endPage; i++)  
        {  
            if (currentPage == i)  
            {  
                sb.Append(" <font color=red> " + i + " </font>&nbsp;&nbsp; ");  
            }  
            else  
            {  
                sb.Append(string.Format(strTemp, i, i, "[" + i + "]"));  
            }  
        }  
  
        if (currentPage != endPage)  
        {  
            sb.Append(string.Format(strTemp, currentPage + 1, currentPage + 1, " 下一頁 "));  
        }  
  
        return sb.ToString();  
    }  
   
          /**/ ///   <summary>   
         ///  計算總頁數  
         ///   </summary>   
         ///   <param name="totalRecords"> 總記錄數 </param>   
         ///   <param name="pageSize"> 每頁記錄數 </param>   
    public static int CalculateTotalPages(int totalRecords, int pageSize)  
    {  
        int totalPagesAvailable;  
  
        totalPagesAvailable = totalRecords / pageSize;  
  
        // 由於C#的整形除法 會把所有餘數舍入為0,所以需要判斷是否需要加1   
        if ((totalRecords % pageSize) > 0)  
            totalPagesAvailable++;  
  
        return totalPagesAvailable;  
    }  
}  
 
Demo2:
 
C#程式碼 
/**//// <summary>  
        /// 類似GOOGLE的分頁函式,王傳煒,資料參考http://www.phpx.com/happy/viewthread.php?tid=99280&extra=&page=2,謝謝YourEyes  
         /// </summary>  
        /// <param name="total">總記錄數</param>  
        /// <param name="per">每頁記錄數</param>  
        /// <param name="page">當前頁數</param>  
        /// <param name="query_string">Url引數</param>  
         private string pagination(int total,int per,int page,string query_string)  
         {  
             int allpage=0;  
             int next=0;  
             int pre=0;  
             int startcount=0;  
             int endcount=0;  
             string pagestr="";   
             if(page<1){page=1;}  
             //計算總頁數  
            if (per != 0)  
             {  
                 allpage = (total / per);  
                 allpage = ((total % per) != 0 ? allpage + 1 : allpage);  
                 allpage = (allpage == 0 ? 1 : allpage);  
             }  
             next=page+1;  
             pre=page-1;  
           startcount=(page+5)>allpage?allpage-9:page-4;//中間頁起始序號  
             //中間頁終止序號  
             endcount = page<5 ? 10 : page+5;  
             if(startcount<1) {startcount=1;} //為了避免輸出的時候產生負數,設定如果小於1就從序號1開始  
             if(allpage<endcount){endcount=allpage;}//頁碼+5的可能性就會產生最終輸出序號大於總頁碼,那麼就要將其控制在頁碼數之內  
             pagestr="共"+ allpage +"頁&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";  
             
             pagestr+=page>1 ?  "<a href=\""+ query_string + "?page=1\">首頁</a>&nbsp;&nbsp;<a href=\""+ query_string +"?page="+ pre +"\">上一頁</a>": "首頁 上一頁";  
              //中間頁處理,這個增加時間複雜度,減小空間複雜度  
              for(int i=startcount;i<=endcount;i++)  
              {   
                  pagestr+=page==i?"&nbsp;&nbsp;<font color=\"#ff0000\">"+i+"</font>":"&nbsp;&nbsp;<a href=\""+ query_string +"?page="+ i +"\">"+ i +"</a>";  
              }  
              pagestr+=page!=allpage ? "&nbsp;&nbsp;<a href=\""+ query_string+"?page="+ next +"\">下一頁</a>&nbsp;&nbsp;<a href=\""+ query_string +"?page="+ allpage+"\">末頁</a>" : " 下一頁 末頁";  
  
             return pagestr;             
         }  
 

相關文章