类似于百度分页效果

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text;

namespace Web
{
    public class Pager
    {
        #region 成员变量
        private int _PageSize;
        private int _RecordCount;
        private int _CurrentPageIndex;
        private int _Offset;//偏移量
        private ArrayList _Urls;
        #endregion

        #region 属性
        public int PageSize
        {
            get
            {
                return _PageSize;
            }
            set
            {
                _PageSize = value;
            }
        }

        public int RecordCount
        {
            get
            {
                return _RecordCount;
            }
            set
            {
                _RecordCount = value;
            }
        }

        public int CurrentPageIndex
        {
            get
            {
                return _CurrentPageIndex;
            }
            set
            {
                _CurrentPageIndex = value;
            }
        }

        public ArrayList Urls
        {
            get
            {
                return _Urls;
            }
        }

        private string keyword;

        public string Keyword
        {
            set { keyword = value; }
        }

        private string sort;

        public string Sort
        {
            set { sort = value; }
        }
        #endregion

        #region 方法
        public Pager()
        {
            _Offset = 10;
            _CurrentPageIndex = 1;
        }
        private void CreateUrls()
        {
            if (_PageSize == 0)
            {
                //new Exception("请设置每页显示记录数!");
                _PageSize = 10;//默认10条
            }
            _Urls = new ArrayList();
            int crrentPageIndex = _CurrentPageIndex;
            int startNum = 0;
            int endNum = 0;
            if (_CurrentPageIndex <= _Offset)//偏移量为10
            {
                startNum = 1;
            }
            else
            {
                startNum = _CurrentPageIndex - _Offset;
            }
            endNum = _CurrentPageIndex + _Offset - 1;//以后可能需要修改;因为限于查出来的总页
            if (endNum > (int)Math.Ceiling((double)_RecordCount / (double)_PageSize))
            {
                endNum = (int)Math.Ceiling((double)_RecordCount / (double)_PageSize);//有余数就加1
            }

            for (int i = startNum; i <= endNum; i++)
            {
                _Urls.Add(i);
            }
        }
        public void DataBind()
        {
            CreateUrls();
        }

        public string Show()
        {
            CreateUrls();
            StringBuilder sb = new StringBuilder();
            if (_CurrentPageIndex > 1)
            {
                int index = _CurrentPageIndex - 1;
                sb.Append("<a href='?keyword=" + keyword + "&sort=" + sort + "&PageIndex=" + index + "'>上一页</a>");
            }
            foreach (int pageIndex in Urls)
            {
                if (_CurrentPageIndex == pageIndex)
                {//当前选中页
                    sb.Append(" <font color='red'>" + pageIndex + "</font>");
                }
                else
                {//其他没被选中的页码
                    sb.Append(" <a href='?keyword=" + keyword + "&sort=" + sort + "&PageIndex=" + pageIndex + "'>[" + pageIndex + "]</a>");
                }
            }
            int pageCount = (int)Math.Ceiling((double)_RecordCount / (double)_PageSize);
            if (_CurrentPageIndex < pageCount)
            {
                int index = _CurrentPageIndex + 1;
                sb.Append(" <a href='?keyword=" + keyword + "&sort=" + sort + "&PageIndex=" + index + "'>下一页</a>");
            }

            return sb.ToString();
        }

        #endregion

    }
}

 



用法:
 Pager pager = new Pager();
 pager.Keyword ="xxx";
 pager.PageSize = 10;
pager.RecordCount=50;

 

转载于:https://www.cnblogs.com/lx0551/archive/2013/05/28/3103020.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值