上一篇寫了一個簡單的自定義分頁控件,當時寫的不夠完善,不能自定義控件的樣式.
現在完善了,望同大家共同探討.
現在已經在網上發布的分頁控件特別多,而且大多都功能特別強,但是之所以選擇自己寫,主要是因為自己寫可
以根據自己的要求來設計,不用的功能就功能免了.
本控件可以自定義樣式,而且傳入的參數和其它常用的分頁控件差不多,記錄總數和一頁的數量,是否自定義樣式,
當頁面數量特別多的時候,可以在分頁信息欄中顯示前幾頁和后幾頁.
具體代碼如下:

Code
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;

}
}
}

控件中有默認的樣式,在分頁欄信息中,省略號前后的分頁數量都是默認值5
調用方法
前臺頁面

Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" EnableViewState ="false" Inherits="Default5" %>
<%@ Register Assembly="pagerControls" Namespace="pagerControls" TagPrefix="cc1" %>
<!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>無標題頁</title>
<link href="listPager.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:WebCustomControlPager iNextCount ="5" iPrevCount ="5" ID="WebCustomControlPager1" runat="server" />
</div>
</form>
</body>
</html>
后臺代碼:

Code
protected void Page_Load(object sender, EventArgs e)

{
this.WebCustomControlPager1.pageUrl = this.Request.Url.ToString();
int iPage = 1;
iPage = this.getiMatchId("page");
if (iPage < 1)

{ iPage = 1; }
this.WebCustomControlPager1.currentPageIndex = iPage;
this.WebCustomControlPager1.iNextCount = 5;
this.WebCustomControlPager1.iPrevCount = 5;
this.WebCustomControlPager1.iRowsCount = 10;
this.WebCustomControlPager1.iRecordCount = 120;
this.WebCustomControlPager1.IsCustomStyle = 1;

}
你只要在解決方案中,添加一個web控件庫,命名為pagerControls
然后把默認創建的類更名為:WebCustomControlPager
將文章的所有代碼復制過去,就可能編譯成一個控件dll
這個dll就可能加入到你的網站中了。