只有二个静态方法,不用在使用中带页码参数,在类中自动获取,很为方便:
GetPagerHTML方法得到显示的页码, 如:首 页 上一页 [1] [2] [3] [4] [5] 下一页 末 页
GetPagerTable方法得到指定页的数据.
调用方法:
DBHelper DB = new DBHelper();
DB.OpenConnection();
string sqlString = "select * From Table_Article";
//得到一个DataTable
DataTable TableB = DB.GetDataTable(sqlString);
//把页码显示到页面上,这一部一定要在Pager.GetPagerTable方法前面;
Label2.Text = Pager.GetPagerHTML(TableB.Rows.Count, 3);
//得到指定页的数据
TableB = Pager.GetPagerTable(TableB, 3);
GridView2.DataSource = TableB;
GridView2.DataBind();
分页类代码如下:
public
class
Pager
{
/// <summary>
/// 显示分类字符串 返回字符如:首页 上一页 [2] [3] [4] [5] [6] 下一页 末页 [页次4/10页] [共30条]
/// </summary>
/// <param name="RecordCount"> 记录总数 </param>
/// <param name="PageSize"> 每页显示多少条 </param>
/// <returns></returns>
public static string GetPagerHTML( int RecordCount, int PageSize)
{
// 当前URL地址所有参数
string QueryString = HttpContext.Current.Request.QueryString.ToString();
// 返回的显示页字符串
string BackPageStr = "" ;
// 总页数
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize > 0 )
{
pageCount = RecordCount / PageSize + 1 ;
}
// 当前页数
string IndexPage = HttpContext.Current.Request.QueryString[ " page " ];
int page = 1 ;
if (IndexPage != null )
{
try
{
page = Convert.ToInt32(IndexPage);
} catch {
page = 1 ;
}
}
if (page > pageCount)
{
page = pageCount;
}
// URL后面的参数
string url = "" ;
string strq = "" ;
string [] arrq = QueryString.Split( ' & ' );
if (arrq.Length > 0 )
{
for ( int i = 0 ; i < arrq.Length; i ++ )
{
string [] arrStrq = arrq[i].Split( ' = ' );
if ( ! arrStrq[ 0 ].Equals( " page " ))
{
// if (!arrStrq[0].Equals(""))
// {
// if (strq.Trim().Equals(""))
// {
// strq = "?" + arrStrq[0] + "=" + arrStrq[1];
// }
// else
// {
// strq += "&" + arrStrq[0] + "=" + arrStrq[1];
// }
// }
strq += arrq[i];
}
}
}
if (strq.Equals( "" ))
{
url += " ?page= " ;
}
else
{
url += " ? " + strq + " &page= " ;
}
// 是否为首页
if (page > 1 )
{
BackPageStr += " <a href=' " + url + ( 1 ).ToString() + " '>首 页</a>\n " ;
BackPageStr += " <a href=' " + url + (page - 1 ).ToString() + " '>上一页</a>\n " ;
}
// 显示当前页的前后5页 [1] [2] [3] [4] [5]
int StartPage;
int EndPage;
if (pageCount > 5 )
{
StartPage = page - 2 ;
EndPage = page + 2 ;
if (StartPage <= 0 ) { StartPage = 1 ; }
if (EndPage >= pageCount) { EndPage = pageCount; }
if ((EndPage - StartPage) < 4 && StartPage == 1 ) { EndPage = StartPage + 4 ; }
if ((EndPage - StartPage) < 4 && EndPage == pageCount) { StartPage = EndPage - 4 ; }
}
else {
StartPage = 1 ;
EndPage = pageCount;
}
for ( int i = StartPage; i <= EndPage; i ++ )
{
if (i == page)
{
BackPageStr += " [<a href=' " + url + i.ToString() + " '><b> " + i.ToString() + " </b></a>] \n " ;
}
else
{
BackPageStr += " [<a href=' " + url + i.ToString() + " '> " + i.ToString() + " </a>] \n " ;
}
}
// 是否为最后一页
if (page < pageCount)
{
BackPageStr += " <a href=' " + url + (page + 1 ).ToString() + " '>下一页</a>\n " ;
BackPageStr += " <a href=' " + url + pageCount.ToString() + " '>末 页</a>\n " ;
}
{
/// <summary>
/// 显示分类字符串 返回字符如:首页 上一页 [2] [3] [4] [5] [6] 下一页 末页 [页次4/10页] [共30条]
/// </summary>
/// <param name="RecordCount"> 记录总数 </param>
/// <param name="PageSize"> 每页显示多少条 </param>
/// <returns></returns>
public static string GetPagerHTML( int RecordCount, int PageSize)
{
// 当前URL地址所有参数
string QueryString = HttpContext.Current.Request.QueryString.ToString();
// 返回的显示页字符串
string BackPageStr = "" ;
// 总页数
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize > 0 )
{
pageCount = RecordCount / PageSize + 1 ;
}
// 当前页数
string IndexPage = HttpContext.Current.Request.QueryString[ " page " ];
int page = 1 ;
if (IndexPage != null )
{
try
{
page = Convert.ToInt32(IndexPage);
} catch {
page = 1 ;
}
}
if (page > pageCount)
{
page = pageCount;
}
// URL后面的参数
string url = "" ;
string strq = "" ;
string [] arrq = QueryString.Split( ' & ' );
if (arrq.Length > 0 )
{
for ( int i = 0 ; i < arrq.Length; i ++ )
{
string [] arrStrq = arrq[i].Split( ' = ' );
if ( ! arrStrq[ 0 ].Equals( " page " ))
{
// if (!arrStrq[0].Equals(""))
// {
// if (strq.Trim().Equals(""))
// {
// strq = "?" + arrStrq[0] + "=" + arrStrq[1];
// }
// else
// {
// strq += "&" + arrStrq[0] + "=" + arrStrq[1];
// }
// }
strq += arrq[i];
}
}
}
if (strq.Equals( "" ))
{
url += " ?page= " ;
}
else
{
url += " ? " + strq + " &page= " ;
}
// 是否为首页
if (page > 1 )
{
BackPageStr += " <a href=' " + url + ( 1 ).ToString() + " '>首 页</a>\n " ;
BackPageStr += " <a href=' " + url + (page - 1 ).ToString() + " '>上一页</a>\n " ;
}
// 显示当前页的前后5页 [1] [2] [3] [4] [5]
int StartPage;
int EndPage;
if (pageCount > 5 )
{
StartPage = page - 2 ;
EndPage = page + 2 ;
if (StartPage <= 0 ) { StartPage = 1 ; }
if (EndPage >= pageCount) { EndPage = pageCount; }
if ((EndPage - StartPage) < 4 && StartPage == 1 ) { EndPage = StartPage + 4 ; }
if ((EndPage - StartPage) < 4 && EndPage == pageCount) { StartPage = EndPage - 4 ; }
}
else {
StartPage = 1 ;
EndPage = pageCount;
}
for ( int i = StartPage; i <= EndPage; i ++ )
{
if (i == page)
{
BackPageStr += " [<a href=' " + url + i.ToString() + " '><b> " + i.ToString() + " </b></a>] \n " ;
}
else
{
BackPageStr += " [<a href=' " + url + i.ToString() + " '> " + i.ToString() + " </a>] \n " ;
}
}
// 是否为最后一页
if (page < pageCount)
{
BackPageStr += " <a href=' " + url + (page + 1 ).ToString() + " '>下一页</a>\n " ;
BackPageStr += " <a href=' " + url + pageCount.ToString() + " '>末 页</a>\n " ;
}
BackPageStr += " [页次" + page.ToString() + "/" + pageCount.ToString() + "页] ";
BackPageStr += " [共" + RecordCount.ToString() + "条] ";
return BackPageStr;
}
/// <summary>
/// 对DataTable进行分页,起始页为1
/// </summary>
/// <param name="dt"> 要显示的数据表 </param>
/// <param name="PageSize"> 每页显示多少条数据 </param>
/// <returns></returns>
public static DataTable GetPagerTable(DataTable dt, int PageSize)
{
// 总条数
int RecordCount = dt.Rows.Count;
// 总页数
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize > 0 )
{
pageCount = RecordCount / PageSize + 1 ;
}
// 当前页数
string IndexPage = HttpContext.Current.Request.QueryString[ " page " ];
int page = 1 ;
if (IndexPage != null )
{
try
{
page = Convert.ToInt32(IndexPage);
}
catch
{
page = 1 ;
}
}
if (page > pageCount)
{
page = pageCount;
}
DataTable newdt = dt.Copy();
newdt.Clear();
int rowbegin = ((page - 1 ) * PageSize);
int rowend = page * PageSize;
if (rowbegin >= RecordCount)
return newdt;
if (rowend > dt.Rows.Count)rowend = dt.Rows.Count;
for ( int i = rowbegin; i <= rowend - 1 ; i ++ )
{
DataRow newdr = newdt.NewRow();
DataRow dr = dt.Rows[i];
foreach (DataColumn column in dt.Columns)
{
newdr[column.ColumnName] = dr[column.ColumnName];
}
newdt.Rows.Add(newdr);
}
return newdt;
}
}
BackPageStr += " [共" + RecordCount.ToString() + "条] ";
return BackPageStr;
}
/// <summary>
/// 对DataTable进行分页,起始页为1
/// </summary>
/// <param name="dt"> 要显示的数据表 </param>
/// <param name="PageSize"> 每页显示多少条数据 </param>
/// <returns></returns>
public static DataTable GetPagerTable(DataTable dt, int PageSize)
{
// 总条数
int RecordCount = dt.Rows.Count;
// 总页数
int pageCount = RecordCount / PageSize;
if (RecordCount % PageSize > 0 )
{
pageCount = RecordCount / PageSize + 1 ;
}
// 当前页数
string IndexPage = HttpContext.Current.Request.QueryString[ " page " ];
int page = 1 ;
if (IndexPage != null )
{
try
{
page = Convert.ToInt32(IndexPage);
}
catch
{
page = 1 ;
}
}
if (page > pageCount)
{
page = pageCount;
}
DataTable newdt = dt.Copy();
newdt.Clear();
int rowbegin = ((page - 1 ) * PageSize);
int rowend = page * PageSize;
if (rowbegin >= RecordCount)
return newdt;
if (rowend > dt.Rows.Count)rowend = dt.Rows.Count;
for ( int i = rowbegin; i <= rowend - 1 ; i ++ )
{
DataRow newdr = newdt.NewRow();
DataRow dr = dt.Rows[i];
foreach (DataColumn column in dt.Columns)
{
newdr[column.ColumnName] = dr[column.ColumnName];
}
newdt.Rows.Add(newdr);
}
return newdt;
}
}