public class PagerModel
{
private PagerInfo _PagerInfo = new PagerInfo();
public PagerInfo PagerInfo
{
get { return _PagerInfo; }
set { _PagerInfo = value; }
}
}
public class PagerInfo
{
public PagerInfo()
{
this.PageSize = 10;
this.PageIndex = 1;
//this.RecordCount = 0;
}
public int PageIndex { get; set; }
public int PageSize { get; set; }
public int RecordCount { get; set; }
public int BeforeCount
{
get
{
return (this.PageIndex - 1) * this.PageSize;
}
}
public void SetRecordCount(int nRecordCount)
{
this.RecordCount = nRecordCount;
}
}
public class PagerPost
{
public PagerPost()
{ }
//public bool IsNew { get; set; }
//public PagerPost(int iPageIndex,)
private int _RecordCount = 0;
/// <summary>
/// 记录数
/// </summary>
public int RecordCount
{
get
{
return _RecordCount;
}
set
{
if (value < 0) throw new Exception("记录数不能为负数");
_RecordCount = value;
CountPageCount();//计算页数
}
}
private int _PageSize = 10;
/// <summary>
/// 页大小
/// </summary>
public int PageSize
{
get
{
return _PageSize;
}
set
{
if (value < 1) throw new Exception("页大小不能小于一");
_PageSize = value;
CountPageCount();//计算页数
}
}
private int _PageIndex = 1;
/// <summary>
/// 页码
/// </summary>
public int PageIndex
{
get
{
return _PageIndex;
}
set
{
if (value < 0) throw new Exception("页码不能为负数");
if (_PageCount > 0)
if (value > _PageCount)
_PageIndex = 1;
else
_PageIndex = value;
}
}
/// <summary>
/// 页数
/// </summary>
private int _PageCount = 0;
/// <summary>
/// 起始显示页数
/// </summary>
private int _DefaultViewPageCount = 10;
/// <summary>
/// 最大显示页数
/// </summary>
private int _MaximumViewPageCount = 11;
/// <summary>
/// 计算页数
/// </summary>
private void CountPageCount()
{
_PageCount = _RecordCount / _PageSize + (_RecordCount % _PageSize == 0 ? 0 : 1);
}
public string RenderHtml(int iPageIndex)
{
this.PageIndex = iPageIndex;
this.RecordCount = _RecordCount;
return RenderHtml();
}
public string RenderHtml()
{
//没有记录时不输出
//if (_PageCount <2) return "";
StringBuilder stbResult = new StringBuilder();
//stbResult.Append("\n<div id='pager'>");
//stbResult.Append("\n<script type='text/javascript'>");
//stbResult.Append("\nfunction PagerPost(iPageIndex){var PageIndex=document.createElement('input');PageIndex.name='PagerInfo.PageIndex';PageIndex.type='hidden';PageIndex.value=iPageIndex;document.forms[0].appendChild(PageIndex);document.forms[0].submit();}");
//stbResult.Append("\n</script>");
stbResult.Append("\n<div id='pager' style='background-color:#E3EFFB;'>");
stbResult.Append("\n<script type='text/javascript'>");
//新增RecordCount 记录数用于判别是否新查询
// stbResult.Append("\nvar IsNew=document.createElement('input');IsNew.name='PagerInfo.IsNew';IsNew.id='HhIsNew';IsNew.type='hidden';IsNew.value='False';document.forms[0].appendChild(IsNew);");
stbResult.Append("\nvar RecordCount=document.createElement('input');RecordCount.name='PagerInfo.RecordCount';RecordCount.type='hidden';RecordCount.value=" + _RecordCount + ";document.forms[0].appendChild(RecordCount);");
stbResult.Append("\nvar PageSize=document.createElement('input');PageSize.name='PagerInfo.PageSize';PageSize.type='hidden';PageSize.value=" + _PageSize + ";document.forms[0].appendChild(PageSize);");
stbResult.Append("\nvar PageIndex=document.createElement('input');PageIndex.name='PagerInfo.PageIndex';PageIndex.type='hidden';PageIndex.value=" + _PageIndex + ";PageIndex.id='hidPageIndex';document.forms[0].appendChild(PageIndex);");
stbResult.Append("\ndocument.forms[0].onsubmit=function(){document.getElementById('hidPageIndex').value=1;return true;}");
stbResult.Append("\nfunction PagerPost(iPageIndex){document.getElementById('hidPageIndex').value=iPageIndex;document.forms[0].submit();}");
//stbResult.Append("\nfunction PagerPost(iPageIndex){var PageSize=document.createElement('input');PageSize.name='PagerInfo.PageSize';PageSize.type='hidden';PageSize.value=" + _PageSize + ";document.forms[0].appendChild(PageSize);var PageIndex=document.createElement('input');PageIndex.name='PagerInfo.PageIndex';PageIndex.type='hidden';PageIndex.value=iPageIndex;document.forms[0].appendChild(PageIndex);document.forms[0].submit();}");
stbResult.Append("\n</script>");
//首页,上一页
if (_PageIndex > 1)
{
stbResult.Append(string.Format("\n<a href='javascript:PagerPost({0})'>首页</a>", 1));
stbResult.Append(string.Format("\n<a href='javascript:PagerPost({0})'>上一页</a>", (_PageIndex - 1)));
}
else
{
stbResult.Append("\n<a style='color:Gray;text-decoration:none;'>首页</a>");
stbResult.Append("\n<a style='color:Gray;text-decoration:none;'>上一页</a>");
}
////数字页 google式分页
//int iBegIndex = 1;
//int iEndIndex = _DefaultViewPageCount;
//if (_PageIndex >= _DefaultViewPageCount)
//{
// iEndIndex = _PageIndex + _DefaultViewPageCount - 1;
//}
//if (iEndIndex > _PageCount)
//{
// iEndIndex = _PageCount;
//}
//if (iEndIndex > _MaximumViewPageCount)
//{
// iBegIndex += _PageIndex - _DefaultViewPageCount - 1;
//}
//固定式10页
int iBegIndex = 1;
if (_PageIndex / _DefaultViewPageCount > 0)
{
if (_PageIndex % _DefaultViewPageCount == 0)
{
iBegIndex = (_PageIndex / _DefaultViewPageCount - 1) * _DefaultViewPageCount + 1;
}
else
{
iBegIndex = _PageIndex / _DefaultViewPageCount * _DefaultViewPageCount + 1;
}
}
int iEndIndex = iBegIndex + _DefaultViewPageCount - 1;
if (iEndIndex > _PageCount)
{
iEndIndex = _PageCount;
}
for (int i = iBegIndex; i <= iEndIndex; i++)
{
if (i == _PageIndex)
{
stbResult.Append(string.Format("\n<a style='color:Gray;text-decoration:none;'>{0}</a>", i));
}
else
{
stbResult.Append(string.Format("\n<a href='javascript:PagerPost({0})'>{1}</a>", i, i));
}
}
//下一页,尾页
if (_PageIndex < _PageCount)
{
stbResult.Append(string.Format("\n<a href='javascript:PagerPost({0})'>下一页</a>", (_PageIndex + 1)));
stbResult.Append(string.Format("\n<a href='javascript:PagerPost({0})'>尾页</a>", _PageCount));
}
else
{
stbResult.Append("\n<a style='color:Gray;text-decoration:none;'>下一页</a>");
stbResult.Append("\n<a style='color:Gray;text-decoration:none;'>尾页</a>");
}
//总页
stbResult.Append(string.Format("\n{0}/{1}<span>共</span>{2}<span>条</span>", _PageIndex, _PageCount, _RecordCount));
stbResult.Append("\n</div>");
return stbResult.ToString();
}
}