分页解决方案

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  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值