public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int p = 1;
try
{
p= Convert.ToInt32(Request.QueryString["page"]);
}
catch (Exception)
{
}
PageTools _page = PageTools.GetPage(p);
for (int i = _page.CurrentlyMinRecord; i <= _page.CurrentlyMaxRecord; i++)
{
ltContent.Text += "第" + i + "条 <br/>";
}
if (_page.HaveFrontPage)
{
ltPager.Text += "上页";
}
for (int i = _page.CurrentlyScreenMin; i <= _page.CurrentlyScreenMax; i++)
{
if (i == _page.CurrentlyPage)
{
ltPager.Text += "当前" + i + "页 ";
}
else
{
ltPager.Text += i + "页 ";
}
}
if (_page.HaveNextPage)
{
ltPager.Text += "下页";
}
}
}
public class PageTools
{
public static PageTools GetPage(int page)
{
PageTools _page = new PageTools() { PerPagerRecord = 10, PerScreenRecord = 10, TotalRecord = 169, CurrentlyPage = page };
if (page > _page.TotalPage)
{
_page.CurrentlyPage = _page.TotalPage;
}
if (page < 0)
{
_page.CurrentlyPage = 1;
}
return _page;
}
/// <summary>
///当前页码
/// </summary>
public int CurrentlyPage { set; get; }
/// <summary>
/// 总记录数
/// </summary>
public int TotalRecord { set; get; }
/// <summary>
/// 每页记录数
/// </summary>
public int PerPagerRecord { set; get; }
/// <summary>
/// 每屏显示页数
/// </summary>
public int PerScreenRecord { set; get; }
#region 计算出的属性值
/// <summary>
/// 总页数
/// </summary>
public int TotalPage
{
get
{
if (TotalRecord > 0)
{
if ((TotalRecord / PerPagerRecord) > 1)
{
return ((TotalRecord % PerPagerRecord) == 0) ? (TotalRecord / PerPagerRecord) : (TotalRecord / PerPagerRecord) + 1;
}
else
{
return 1;
}
}
else
{
return 0;
}
}
}
/// <summary>
/// 总屏数
/// </summary>
public int TotalScreen
{
get
{
return TotalRecord % (PerScreenRecord * PerPagerRecord) > 0 ? TotalRecord / (PerScreenRecord * PerPagerRecord) + 1 : TotalRecord / (PerScreenRecord * PerPagerRecord);
}
}
/// <summary>
/// 当前页最大记录数
/// </summary>
public int CurrentlyMaxRecord
{
get
{
if (CurrentlyPage <= 0)//当前页码<=0
{
return (TotalRecord > PerPagerRecord) ? PerPagerRecord : TotalRecord;
}
else if (CurrentlyPage >= TotalPage)//当前页码>总页码
{
return TotalRecord;
}
else//正常
{
return (TotalRecord < PerPagerRecord) ? TotalRecord : CurrentlyPage * PerPagerRecord;
}
}
}
/// <summary>
/// 当前页最小记录数
/// </summary>
public int CurrentlyMinRecord
{
get
{
if (CurrentlyPage <= 0)//当前页码<=0
{
return 1;
}
else if (CurrentlyPage > TotalPage)//当前页码>总页码
{
return (TotalPage - 1) * PerPagerRecord + 1;
}
else//正常
{
return (CurrentlyPage - 1) * PerPagerRecord + 1;
}
}
}
/// <summary>
/// 当前屏最小页码
/// </summary>
public int CurrentlyScreenMin
{
get
{
if (CurrentlyPage <= 1)//当前页码<=0
{
return (TotalRecord > 0) ? 1 : 0;
}
else if (CurrentlyPage >= TotalPage)//当前页码>总页码
{
return (TotalScreen - 1) * PerScreenRecord + 1;
}
else//正常
{
return CurrentScreen % PerScreenRecord == 0 ? (CurrentScreen - 1) * PerScreenRecord : (CurrentScreen - 1) * PerScreenRecord + 1;
}
}
}
/// <summary>
/// 当前屏最大页码
/// </summary>
public int CurrentlyScreenMax
{
get
{
if (CurrentlyPage <= 0)//当前页码<=0
{
return PerPagerRecord > TotalRecord ? TotalRecord : PerPagerRecord;
}
else if (CurrentlyPage >= TotalPage)//当前页码>总页码
{
return TotalPage;
}
else//正常
{
return TotalPage > CurrentScreen * PerScreenRecord ? CurrentScreen * PerScreenRecord : TotalPage;
}
}
}
/// <summary>
/// 当前屏码
/// </summary>
public int CurrentScreen
{
get
{
return (CurrentlyPage / PerScreenRecord) + 1;
}
}
/// <summary>
/// 是否有 上一页
/// </summary>
public bool HaveFrontPage
{
get
{
return CurrentScreen > 1;
}
}
/// <summary>
/// 是否有 下一页
/// </summary>
public bool HaveNextPage
{
get
{
return CurrentScreen < TotalScreen;
}
}
#endregion
}
分页解决方案
最新推荐文章于 2024-08-24 09:49:56 发布