类似baidu google分页页码效果的代码
以后做图片网站用过,现在只写出代码,其实没什么技术含量,只是最近工作不顺,心情稍差,思维有点懒惰,工作的时候需要抄过来用用OK,所才Post在此.
/// <summary>
/// 创建并显示分页器
/// </summary>
private void BuildPager(int totalRecords,int currentPage,int pageSize,int pid)
{
int alter = 4 ;
int startPage = 1 ;
int endPage = currentPage + alter ;
int totalPages = this.CalculateTotalPages(totalRecords,pageSize) ;
if(currentPage > alter)
{
startPage = currentPage - alter ;
}
if(endPage > totalPages)
{
endPage = totalPages ;
}
string strTemp = @"<a href='PhotoList.aspx?pid={0}&pno={1}'>{2}</a> " ;
StringBuilder sb = new StringBuilder("") ;
if(currentPage != startPage)
{
sb.Append( string.Format( strTemp , pid , 1 , "上一页" ) ) ;
}
for( int i = startPage ; i <= endPage ; i++ )
{
if( currentPage == i )
{
sb.Append("<font color=red>" + i + "</font> ") ;
}
else
{
sb.Append( string.Format( strTemp , pid , i ,"[" + i + "]" ) ) ;
}
}
if(currentPage != endPage)
{
sb.Append( string.Format( strTemp , pid , currentPage + 1 , "下一页") ) ;
}
this.ltlShowPager.Text = sb.ToString() ;
}
/// <summary>
/// 计算总页数
/// </summary>
/// <param name="totalRecords">总记录数</param>
/// <param name="pageSize">每页记录数</param>
private int CalculateTotalPages(int totalRecords, int pageSize)
{
int totalPagesAvailable;
totalPagesAvailable = totalRecords / pageSize;
//由于C#的整形除法 会把所有余数舍入为0,所以需要判断是否需要加1
if ((totalRecords % pageSize) > 0)
totalPagesAvailable++;
return totalPagesAvailable;
}
/// 创建并显示分页器
/// </summary>
private void BuildPager(int totalRecords,int currentPage,int pageSize,int pid)
{
int alter = 4 ;
int startPage = 1 ;
int endPage = currentPage + alter ;
int totalPages = this.CalculateTotalPages(totalRecords,pageSize) ;
if(currentPage > alter)
{
startPage = currentPage - alter ;
}
if(endPage > totalPages)
{
endPage = totalPages ;
}
string strTemp = @"<a href='PhotoList.aspx?pid={0}&pno={1}'>{2}</a> " ;
StringBuilder sb = new StringBuilder("") ;
if(currentPage != startPage)
{
sb.Append( string.Format( strTemp , pid , 1 , "上一页" ) ) ;
}
for( int i = startPage ; i <= endPage ; i++ )
{
if( currentPage == i )
{
sb.Append("<font color=red>" + i + "</font> ") ;
}
else
{
sb.Append( string.Format( strTemp , pid , i ,"[" + i + "]" ) ) ;
}
}
if(currentPage != endPage)
{
sb.Append( string.Format( strTemp , pid , currentPage + 1 , "下一页") ) ;
}
this.ltlShowPager.Text = sb.ToString() ;
}
/// <summary>
/// 计算总页数
/// </summary>
/// <param name="totalRecords">总记录数</param>
/// <param name="pageSize">每页记录数</param>
private int CalculateTotalPages(int totalRecords, int pageSize)
{
int totalPagesAvailable;
totalPagesAvailable = totalRecords / pageSize;
//由于C#的整形除法 会把所有余数舍入为0,所以需要判断是否需要加1
if ((totalRecords % pageSize) > 0)
totalPagesAvailable++;
return totalPagesAvailable;
}