asp.net分页控件

建立一个用户控件:Pager.ascx,然后后台代码修改为:
  1public partial class Pager : System.Web.UI.UserControl
  2    {
  3        private string _UrlFormat;
  4        private int _PageSize = 10;
  5        private int _RecordCount;
  6        private int _PageCount = 5;
  7
  8        /// <summary>
  9        /// 连接格式
 10        /// </summary>
 11        public string UrlFormat
 12        {
 13            get
 14            {
 15                return _UrlFormat;
 16            }
 17            set
 18            {
 19                _UrlFormat = value;
 20            }
 21        }
 22
 23        /// <summary>
 24        /// 页长度
 25
 26
 27        /// </summary>
 28        public int PageSize
 29        {
 30            get
 31            {
 32                return _PageSize;
 33            }
 34            set
 35            {
 36                _PageSize = value;
 37            }
 38        }
 39
 40        /// <summary>
 41        /// 当前页码
 42        /// </summary>
 43        public int PageIndex
 44        {
 45            get
 46            {
 47                string Pageindex = HttpContext.Current.Request.QueryString["i"];
 48                if (Pageindex != null)
 49                {
 50                    return int.Parse(Pageindex);
 51                }
 52                return 1;
 53            }
 54        }
 55
 56        /// <summary>
 57        /// 总记录数
 58        /// </summary>
 59        public int RecordCount
 60        {
 61            get
 62            {
 63                return _RecordCount;
 64            }
 65            set
 66            {
 67                _RecordCount = value;
 68            }
 69        }
 70
 71        /// <summary>
 72        /// 两边显示个数
 73        /// </summary>
 74        public int PageCount
 75        {
 76            get
 77            {
 78                return _PageCount;
 79            }
 80            set
 81            {
 82                _PageCount = value;
 83            }
 84        }
 85
 86        protected override void Render(HtmlTextWriter writer)
 87        {
 88            if (RecordCount == 0)
 89                return;
 90            int SumPage = (RecordCount + PageSize - 1) / PageSize;
 91
 92            int start = PageIndex - PageCount;
 93            int end = PageIndex + PageCount;
 94
 95            //以PageIndex为中心,前后个显示Page个页码导航
 96
 97
 98            if (SumPage > (PageCount * 2 + 1))
 99            {
100                if (start < 1)
101                {
102                    start = 1;
103                    end = start + 10;
104                }
105                else if (end > SumPage)
106                {
107                    start = SumPage - 10;
108                    end = SumPage;
109                }
110            }
111            else
112            {
113                start = 1;
114                end = SumPage;
115            }
116
117
118
119            string tmp = "<a href=/"" + UrlFormat + "/">[{0}]</a>";
120            StringBuilder sb = new StringBuilder(string.Format("页次:{0}/{1}  每页:{2}  共计:{3} 条 ", PageIndex, SumPage, PageSize, RecordCount));
121            if (PageIndex > 1)
122            {
123                sb.Append(string.Format("<a href=/"" + UrlFormat + "/">首页</a> ", 1));
124                sb.Append(string.Format(" <a href=/"" + UrlFormat + "/">上一页</a> ", PageIndex - 1));
125            }
126            for (int i = start; i <= end; i++)
127            {
128                if (i == PageIndex)
129                {
130                    sb.Append(" <strong>" + PageIndex.ToString() + "</strong> ");
131                }
132                else
133                {
134                    sb.Append(string.Format(tmp, i));
135                }
136                sb.Append("&nbsp;");
137            }
138            if (PageIndex < SumPage)
139            {
140                sb.Append(string.Format(" <a href=/"" + UrlFormat + "/">下一页</a> ", PageIndex + 1));
141                sb.Append(string.Format(" <a href=/"" + UrlFormat + "/">尾页</a>", SumPage));
142            }
143            writer.Write(sb.ToString());
144        }
145        protected void Page_Load(object sender, EventArgs e)
146        {
147
148        }
149    }使用方法:
把Pager拖拽到页面上,进入页面后台代码,设置如下:
 
1            Pager1.UrlFormat = "?i={0}";//分页格式
2            int recordcount, pagecount;
3            Repeater1.DataSource = 数据源;
4            Repeater1.DataBind();
5            Pager1.RecordCount = recordcount;
6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值