上一篇寫了一個簡單的自定義分頁控件,當時寫的不夠完善,不能自定義控件的樣式.現在完善了,望同 大家共同探討.
現在已經在網上發布的分頁控件特別多,而且大多都功能特別強,但是之所以選擇自己寫,主要是因為自 己寫可以根據自己的要求來設計,不用的功能就功能免了.
本控件可以自定義樣式,而且傳入的參數和其它常用的分頁控件差不多,記錄總數和一頁的數量,是否自 定義樣式,當頁面數量特別多的時候,可以在分頁信息欄中顯示前幾頁和後幾頁.
具體代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace pagerControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControlPager runat=server></{0}:WebCustomControlPager>")]
public class WebCustomControlPager : WebControl
{
private string _pageUrl="";
/**//// <summary>
/// 當前頁面地址
/// </summary>
[Bindable(true)]
[CategoryAttribute("Appearance")]
[DefaultValue("")]
[Localizable(true)]
[DescriptionAttribute("當前頁面地址")]
public virtual string pageUrl
{
get
{
return this._pageUrl;
}
set
{
this ._pageUrl = value;
}
}
private int _currentPageIndex;
/**//// <summary>
/// 當前頁面索引
/// </summary>
[Bindable(true)]
[CategoryAttribute("Appearance")]
[Localizable(true)]
[DescriptionAttribute("當前頁面索引從1開始")]
[DefaultValueAttribute("當前頁面索引")]
public virtual int currentPageIndex
{
get
{
return this._currentPageIndex;
}
set
{
this._currentPageIndex = value;
}
}
private int _IsCustomStyle;
/**//// <summary>
/// 是否自定義樣式
/// 0:不是 1:是
/// </summary>
[Bindable(true)]
[CategoryAttribute("Appearance")]
[Localizable(true)]
[DescriptionAttribute("是否自定義樣式")]
[DefaultValueAttribute("0")]
public virtual int IsCustomStyle
{
get
{
return this._IsCustomStyle;
}
set
{
this._IsCustomStyle = value;
}
}
private int _iRecordCount=1;
/**//// <summary>
/// 記錄數量
/// </summary>
[Bindable(true)]
[CategoryAttribute("Appearance")]
[Localizable(true)]
[DescriptionAttribute("記錄數量")]
[DefaultValueAttribute("記錄數量")]
public virtual int iRecordCount
{
get
{
return this._iRecordCount;
}
set
{
this._iRecordCount = value;
}
}
private int _iRowsCount=10;
/**//// <summary>
/// 每頁記錄數量
/// </summary>
[Bindable(true)]
[CategoryAttribute ("Appearance")]
[Localizable(true)]
[DescriptionAttribute("每頁記錄數量")]
[DefaultValueAttribute("每頁記錄數量")]
public virtual int iRowsCount
{
get
{
return this._iRowsCount;
}
set
{
this._iRowsCount = value;
}
}
private int _iPrevCount=5;
/**//// <summary>
/// 前部分記錄數量
/// </summary>
[Bindable(true)]
[CategoryAttribute("Appearance")]
[Localizable(true)]
[DescriptionAttribute("前部分記錄數量")]
[DefaultValueAttribute("前部分記錄數量")]
public virtual int iPrevCount
{
get
{
return this._iPrevCount;
}
set
{
this._iPrevCount = value;
}
}
private int _iNextCount=5;
/**//// <summary>
/// 後部分記錄數量
/// </summary>
[Bindable(true)]
[CategoryAttribute("Appearance")]
[Localizable(true)]
[DescriptionAttribute("後部分記錄數量")]
[DefaultValueAttribute("後部分記錄數量")]
public virtual int iNextCount
{
get
{
return this._iNextCount ;
}
set
{
this._iNextCount = value;
}
}
/**//// <summary>
/// 取得默認樣式表信息
/// by minjiang 08-3-24
/// </summary>
/// <returns></returns>
private string styleHtml()
{
//分頁樣式表信息
string sStyle = "";
StringBuilder strbStyle = new StringBuilder();
sStyle = "<style type =\"text/css\" >";
strbStyle.Append(sStyle);
樣式內容#region 樣式內容
sStyle = ".a4:link,.a4:visited,.a4:active {color:#207FC3;font-size:12px;text-decoration:none;}";
strbStyle.Append (sStyle);
sStyle = ".a4:hover{color:#ff6600;font-size:12px;text- decoration:none;}";
strbStyle.Append(sStyle);
sStyle = ".a5:link,.a5:visited,.a5:active{color:#ffffff;font-size:12px;text-decoration:none;} ";
strbStyle.Append(sStyle);
sStyle = ".a5:hover {color:#ffffff;font-size:12px;text-decoration:none;}";
strbStyle.Append (sStyle);
sStyle = ".survey_pagediv {float:left;width:950px;height:22px; margin-top:15px;}";
strbStyle.Append(sStyle);
sStyle = ".survey_pagediv .pagedivcenter { width:600px; margin: 0 auto;}";
strbStyle.Append(sStyle);
sStyle = ".survey_pagediv .page { float:left;width:auto;font-family: Verdana, Arial, Helvetica, sans-serif;}";
strbStyle.Append(sStyle);
sStyle = ".survey_pagediv .page .select {float:left;height:16px;line-height:16px;padding:0 4px 0 4px;display:block;border:solid 1px #207FC3;margin:0 2px 0 2px;background-color:#207FC3;}";
strbStyle.Append(sStyle);
sStyle = ".survey_pagediv .page .num {float:left;height:16px;line-height:16px;padding:0 4px 0 4px;display:block;border:solid 1px #207FC3;margin:0 2px 0 2px;}";
strbStyle.Append(sStyle);
sStyle = ".survey_pagediv .page span{float:left;height:18px;line- height:18px;display:block;margin:0 4px 0 4px;}";
strbStyle.Append (sStyle);
sStyle = ".survey_pagediv_nodiv { float:left; width:230px; height:auto; border:solid 1px #C2E8C7; background-color:#FBFFFB; padding:5px;}";
strbStyle.Append(sStyle);
sStyle = ".survey_pagediv_nodiv1 { float:left; width:230px; height:155px; border:solid 1px #FFD4E3; background-color:#FFFCFE; padding:5px; text-align:center; line-height:155px;}";
strbStyle.Append (sStyle);
#endregion
sStyle = "</style>";
strbStyle.Append(sStyle);
//樣式內容結尾
sStyle = strbStyle.ToString();
return sStyle;
}
protected override void RenderContents(HtmlTextWriter output)
{
//分頁樣式表 信息
string sStyle = "";
if (this.IsCustomStyle == 0)
{
sStyle = this.styleHtml();
}
//分頁信息
string sPagerHtml = "";
sPagerHtml = this.CSgetPagerHtml(this .iRecordCount , this .iRowsCount , this.pageUrl , this .currentPageIndex );
sPagerHtml = sStyle + sPagerHtml;
output.Write(sPagerHtml);
}
/**//// <summary>
/// 取 得分頁信息
/// by minjiang 08-3-11
/// </summary>
/// <param name="recordCount">記錄總數</param>
/// <param name="iRowsCount">每頁記錄大小</param>
/// <param name="pageUrl">頁面地址</param>
/// <returns></returns>
public string CSgetPagerHtml(int recordCount, int iRowsCount, string pageUrl, int iCurrentPageIndex)
{
int intPrevPageCount = this .iPrevCount ;
int intNextPageCount = this .iNextCount ;
//如果小於此數字則不顯示省略號
int intDefaultCount = this .iNextCount +this .iPrevCount ;
StringBuilder strb = new StringBuilder();
string info = "";
info = "<div class=\"survey_pagediv\"><div class=\"pagedivcenter\"><div class=\"page\">";
strb.Append(info);
//鏈接 地址
if (pageUrl.IndexOf("page=") != -1)
{
int pageIndex = pageUrl.IndexOf("page=");
pageUrl = pageUrl.Substring(0, pageIndex + 5);
}
else
{
//如果頁面沒有任何參數則加上?與參數
if (pageUrl.IndexOf("?") == -1)
{
pageUrl += "?page=";
}
else
{
//如果只是沒有page參數則加上些參數
pageUrl += "&page=";
}
}
//頁面總數
int pageCount = 1;
if (recordCount > 0)
{
if (recordCount % iRowsCount == 0)
{ pageCount = recordCount / iRowsCount; }
else
{
if (recordCount>iRowsCount)
{
//如果頁面數 量大於1
pageCount = recordCount / iRowsCount + 1;
}
else
{
// 不顯示分頁信息
return "";
}
}
}
if (pageCount < 1)
{
//如果只有一頁則不顯示分頁信息
return "";
}
int currentPage = iCurrentPageIndex ;
//int.TryParse (sCurrentPageIndex, out currentPage);
if (currentPage < 1)
{
currentPage = 1;
}
//上一頁索引
int prevPage = 1;
prevPage = currentPage - 1;
if (prevPage < 1)
{ prevPage = 1; }
//下一頁索引
int nextPage = 1;
nextPage = currentPage + 1;
if (nextPage > pageCount)
{ nextPage = 1; }
//開始的索引頁
int startPageIndex = currentPage;
//要統計的頁面總數
int pageTotalCount = pageCount - currentPage + 1;
if (currentPage != 1)
{
//如果當前頁面不是首頁則顯示上一頁
info = "<span><a href=\"" + pageUrl + prevPage.ToString() + "\"><<上一頁 </a></span>";
strb.Append(info);
}
//回首頁
info = "<span><a href=\"" + pageUrl + "1\">首頁 </a></span>";
strb.Append(info);
string pageClass = "num a4";
//當前頁的樣式為select a5
//如 果頁數小於等於10頁則不顯示省略號
if (pageCount <= intDefaultCount)
{
for (int kk = 1; kk <= pageCount; kk++)
{
pageClass = "num a4";
if (kk == currentPage)
{
pageClass = "select a5";
}
info = "<a href=\"" + pageUrl + kk.ToString() + "\" class=\"" + pageClass + "\">" + kk.ToString() + "</a>";
strb.Append(info);
}
//下一頁地址
//info = "<span><a href=\"" + pageUrl + nextPage.ToString() + "\">下一頁>></a></span>";
//strb.Append(info);
info = strb.ToString();
return info;
}
//省略號前顯示5頁
if (pageTotalCount <= intDefaultCount )
{
//如果要統計的頁面數量小於分頁前 部分數量加分頁後部分數量
//要統計的頁面數量與分頁前部分數量加分頁後部分數 量的差額
//分頁欄總是顯示分頁前部分數量加分頁後部分數量個頁面鏈接
int iDispersion = intDefaultCount - pageTotalCount;
for (int k = currentPage-iDispersion ; k <= pageCount; k++)
{
pageClass = "num a4";
if (k == currentPage)
{
pageClass = "select a5";
}
info = "<a href=\"" + pageUrl + k.ToString() + "\" class=\"" + pageClass + "\">" + k.ToString() + "</a>";
strb.Append(info);
}
}
else
{
for (int k = currentPage; k <= currentPage + intPrevPageCount - 1; k++)
{
pageClass = "num a4";
if (k == currentPage)
{
pageClass = "select a5";
}
info = "<a href=\"" + pageUrl + k.ToString() + "\" class=\"" + pageClass + "\">" + k.ToString() + "</a>";
strb.Append(info);
}
if (pageTotalCount > (intPrevPageCount + intNextPageCount))
{
//如果要統計的頁面總數大於省略號前的記錄加省略號後面的記錄數量則 顯示 ""
info = " <span></span>";
strb.Append(info);
}
//開始索引
int _iNextSatrIndex = pageCount - intNextPageCount+1;
for (int j = _iNextSatrIndex; j <= pageCount; j++)
{
pageClass = "num a4";
if (j == currentPage)
{
pageClass = "select a5";
}
info = "<a href=\"" + pageUrl + j.ToString() + "\" class=\"" + pageClass + "\">" + j.ToString() + "</a>";
strb.Append(info);
}
}
//回末頁
//info = "<span><a href=\"" + pageUrl +pageCount .ToString ()+ "\"> 末頁 </a></span>";
//strb.Append(info);
if (currentPage != pageCount)
{
//如果不是最後一頁則顯示下一 頁
info = "<span><a href=\"" + pageUrl + nextPage.ToString() + "\">下一頁 >></a></span>";
strb.Append(info);
}
info = "</div></div></div>";
strb.Append(info);
info = strb.ToString();
return info;
}
}
}