自定义控件(分页)

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls.Adapters;
namespace LBControl
{
    public class LBPager : Control, IPostBackEventHandler
    {

        //页大小
        private int pagesize = 10;
        public int PageSize
        {
            get { return pagesize; }
            set { pagesize = value; }
        }

        //总数量
        private int recordcount;
        public int RecordCount
        {
            get { return recordcount; }
            set { recordcount = value; }
        }

        // 总页数
        private int PageCount
        {
            get
            {
                int _pageCount = 0;
                if ((recordcount % pagesize) == 0)
                {
                    _pageCount = recordcount / pagesize;
                }
                else
                {
                    _pageCount = recordcount / pagesize + 1;
                }
                return _pageCount;
            }
        }

        //当前页
        private int curpage;
        public int CurPage
        {
            get { return curpage; }
            set { curpage = value; }
        }

        //风格
        private string pastyle;
        public string PAStyle
        {
            get { return pastyle; }
            set { pastyle = value; }
        }

        //显示页数量
        private int showpagecount = 10;
        public int ShowPageCount
        {
            get { return showpagecount; }
            set { showpagecount = value; }
        }


        protected override void Render(HtmlTextWriter writer)
        {
            //得到正确的当前页
            int pacurpage = curpage;
            if (ViewState["cp"] != null)
            {
                pacurpage = int.Parse(ViewState["cp"].ToString());
            }
            pacurpage = pacurpage <= 0 ? 1 : pacurpage;
            pacurpage = pacurpage > PageCount ? PageCount : pacurpage;

            //定义要显示的开始页和结束页.
            int startpageindex = 1;
            int endpageindex = 1;

            if ((PageCount - pacurpage + 1) < showpagecount)
            {

                startpageindex = PageCount - showpagecount + 1;
                startpageindex = startpageindex <= 0 ? 1 : startpageindex;
                endpageindex = PageCount;
            }
            else
            {

                int mo = pacurpage / showpagecount;
                int yu = pacurpage % showpagecount;

                if (yu == 0)
                {
                    mo = mo - 1;
                }

                //如果是最后一页。摸会比其他多1
                startpageindex = mo * showpagecount + 1;
                endpageindex = (mo + 1) * showpagecount;
            }

            writer.Write("<div class=\"" + pastyle + "\">\n");
            writer.Write("记录数:");
            writer.Write(recordcount);
            writer.Write("总页数:");
            writer.Write(PageCount + "");

            writer.Write(" <a href=\"" + this.Page.ClientScript.GetPostBackClientHyperlink(this, "1") + "\"><<</a>\n");

            if (pacurpage != 1)
            {
                writer.Write(" <a href=\"" + this.Page.ClientScript.GetPostBackClientHyperlink(this, (pacurpage - 1).ToString()) + "\">上一页</a>\n");
            }
            else
            {
                writer.Write("<a>上一页</a>\n");
            }

            for (int i = startpageindex; i <= endpageindex; i++)
            {
                if (i == pacurpage)
                {
                    writer.Write(" <a href=\"" + this.Page.ClientScript.GetPostBackClientHyperlink(this, i.ToString()) + "\">[" + i + "]</a>\n");
                }
                else
                {
                    writer.Write(" <a href=\"" + this.Page.ClientScript.GetPostBackClientHyperlink(this, i.ToString()) + "\">" + i + "</a>\n");
                }
            }
            if (pacurpage != PageCount)
            {
                writer.Write(" <a href=\"" + this.Page.ClientScript.GetPostBackClientHyperlink(this, (pacurpage + 1).ToString()) + "\">下一页</a>\n");
            }
            else
            {
                writer.Write(" <a>上一页</a>\n");
            }

            writer.Write(" <a href=\"" + this.Page.ClientScript.GetPostBackClientHyperlink(this, PageCount.ToString()) + "\">>></a>\n");
            writer.Write("</div>\n");
            base.Render(writer);
        }

        public delegate void EventPageIndex(Object sender, pagerarg e);

        public event EventPageIndex PageChange;  //页更改事件

        public virtual void HEREPageChange(Object sender, pagerarg e)
        {
            if (PageChange != null)
            {
                PageChange(this, e);
            }
        }


        public class pagerarg : EventArgs
        {
            private int curpage;
            public int CurPage
            {
                get { return curpage; }
                set { curpage = value; }
            }
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            pagerarg e = new pagerarg();
            e.CurPage = int.Parse(eventArgument);
            curpage = int.Parse(eventArgument);
            ViewState["cp"] = int.Parse(eventArgument);
            HEREPageChange(this, e);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }
    }
}

 

 css:

.paginator
{
 font: 11px Arial, Helvetica, sans-serif;padding:5px 10px 5px 0; margin: 0px;
}
.paginator a
{
 padding: 1px 6px;
 border: solid 1px #ddd;
 background: #fff;
 text-decoration: none;
 margin-right:2px
}
.paginator a:visited
{
 padding: 1px 6px;
 border: solid 1px #ddd;
 background: #fff;
 text-decoration: none;}
.paginator .cpb
{
 padding: 1px 6px;
 font-weight: bold;
 font-size: 13px;
 border:none}
.paginator a:hover
 {
 color: #fff;
 background: #8db2e3;
 border-color:#003366;
 text-decoration: none;
}

原文地址:https://www.cnblogs.com/lsfv/p/1446403.html