分页控件
namespace Web.enterprise.modules
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Page 的摘要说明。
/// </summary>
public class PageList : System.Web.UI.UserControl
{
private const int MAX_PAGE = 5;
private string _url;
private int _currentPage; //当前页
private int _pageCount; //页的总数
private int _base = 0;
protected System.Web.UI.WebControls.Label lblNoCount;
protected System.Web.UI.WebControls.Label lblPageNum;
protected System.Web.UI.WebControls.Label lblList;
protected System.Web.UI.WebControls.Label linkNext;
protected System.Web.UI.WebControls.Label linkPervious;
/// <summary>
/// 连接的Url,不包括分页的连接
/// </summary>
public string Url
{
get
{
return _url;
}
set
{
_url = value;
}
}
/// <summary>
/// 当前的页
/// </summary>
public int CurrentPage
{
get
{
return _currentPage;
}
set
{
_currentPage = value;
}
}
/// <summary>
/// 总的页数
/// </summary>
public int PageCount
{
get
{
return _pageCount;
}
set
{
_pageCount = value;
}
}
//初始化第1页的值为0
public int PageBase
{
get
{
return _base;
}
set
{
if (value !=0 )
{
_base = 1;
}
else
{
_base = 0;
}
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if ( !IsPostBack )
{
InitPage();
}
}
#region 分页
public void InitPage( )
{
//总的页数
linkPervious.Visible = false;
linkNext.Visible = false;
lblList.Visible = false;
lblPageNum.Visible = false;
if( PageCount>0 )
{
linkPervious.Visible=true;
linkNext.Visible=true;
lblList.Visible = true;
lblPageNum.Visible = true;
lblNoCount.Visible = false;
//如果当前页已经超出了总的页数,设置为最后一页
if( _currentPage >= _pageCount )
{
_currentPage = _pageCount - 1 ;
}
//如果当前页小于0,则设置为第一页
if( _currentPage<_base )
{
_currentPage= _base;
}
//最后一页不显示下一页的连接
if ( _currentPage == _pageCount -1)
{
linkNext.Visible= false;
}
//第一页不显示上一页的连接
if ( _currentPage == _base )
{
linkPervious.Visible = false;
}
//初始化页导航
string listtext="";
//要求仅显示10个页码
int minPage=_currentPage-MAX_PAGE;
int maxPage=_currentPage+MAX_PAGE;
//如果最小的页数已经少于0则初始化为0
if( minPage < _base )
{
minPage = _base;
//一开始也显示10个
maxPage = MAX_PAGE * 2;
}
//如果最大的页数已经超过了总页数,则初始化为总页数
if( maxPage > _pageCount )
{
maxPage = _pageCount ;
//后来显示10个
if( _pageCount >= ( MAX_PAGE * 2) )
{
minPage =_pageCount - ( MAX_PAGE * 2) ;
}
}
//循环显示列表
for( int i=minPage ; i<= maxPage - 1;i++ )
{
if( i == _currentPage )
{
if (_base==0)
{
listtext=listtext+" <span class='CurrentPage'>" + ( i + 1 ) + "</span>";
}
else
{
listtext=listtext+" <span class='CurrentPage'>" + i + "</span>";
}
}
else
{
if ( _base == 0 )
{
listtext=listtext+" "+"<a class='PageList' href='" + SetPage(i) + "'>" +(i+1)+ "</a>";
}
else
{
listtext=listtext+" "+"<a class='PageList' href='" + SetPage(i) + "'>" +i+ "</a>";
}
}
}
lblList.Text =listtext;
linkPervious.Text = "<a class='PageList' href='" + SetPage(_currentPage-1) + "'>上一页</a>";
linkNext.Text = "<a class='PageList' href='" + SetPage(_currentPage+1) + "'>下一页</a>";+
lblPageNum.Text = "第" + Convert.ToString(_currentPage+1)+"/" + PageCount + "页";
}
else
{
lblNoCount.Visible = true;
}
}
private string SetPage( int page )
{
string retString = _url;
retString = _url + "CurrentPage=" + page;
return retString;
}
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}