using
System;
using
System.Web;
using
System.Text;
///
<summary>
///
Summary description for PageHelper
///
</summary>
public
class
PageHelper
{
///
<summary>
///
根据当前页码检查分页要显示那一段的分页页码,返回当前页面要显示的第一个页码
///
</summary>
///
<param name="curPage">
当前页码
</param>
///
<returns></returns>
private
int
CheckPage(
int
curPage)
{
string
curpage_str
=
curPage.ToString();
int
FirstpageChart
=
0
;
if
(curpage_str.Length
==
1
)
{
return
1
;
}
else
{
string
tml
=
curpage_str.Substring(
0
, curpage_str.Length
-
1
);
FirstpageChart
=
Convert.ToInt32(tml);
/**/
/*
如果当前是第十页(或二十页)就会有这些情况,本来要到前十页的却到了十到二十页
* 当取得的页码第一位的值乘十后等于当前页码,就把取得的页码第一位减一后乘十返回
* 否则就返回取得的页码第一位的值乘十的值
*/
return
FirstpageChart
*
10
==
curPage
?
(curPage
-
9
) : (FirstpageChart
*
10
+
1
);
}
}
///
<summary>
///
生成页面字符
///
</summary>
///
<param name="countPage">
总页数
</param>
///
<param name="curPage">
当前页
</param>
///
<param name="url">
页面url地址如“xxx.aspx”
</param>
///
<returns></returns>
public
string
HtmlPage(
int
curPage,
int
countPage,
string
url,
string
paramName)
{
StringBuilder htm
=
new
StringBuilder();
int
lastpage
=
CheckPage(curPage);
int
Num
=
lastpage
+
9
;
if
(Num
<=
countPage)
{
for
(
int
i
=
0
; i
<
10
; i
++
)
{
htm.AppendFormat(
"
{0}
"
, Format(lastpage
+
i, curPage, url, paramName));
}
}
else
if
(Num
>
countPage)
{
int
Count
=
countPage
-
(lastpage
-
1
)
*
10
;
for
(
int
i
=
0
; i
<
Count; i
++
)
{
htm.AppendFormat(
"
{0}
"
, Format(lastpage
+
i, curPage, url, paramName));
}
}
return
htm.ToString();
}
///
<summary>
///
格式化页码
///
</summary>
///
<param name="i">
页码
</param>
///
<param name="curPage">
当前页
</param>
///
<param name="url">
页面url地址如“xxx.aspx”
</param>
///
<param name="paramName">
参数名称
</param>
///
<returns></returns>
protected
virtual
string
Format(
int
i,
int
curPage,
string
url,
string
paramName)
{
return
curPage
==
i
?
string
.Format(
"
{0}
"
, i) :
string
.Format(
"
<a href='{0}?{1}={2}'>{3}</a>
"
,
url, paramName, i, i);
}
}