zoukankan      html  css  js  c++  java
  • Pager 精简的分页控件

    建立一个用户控件:Pager.ascx,然后后台代码为:

      public partial class Pager : System.Web.UI.UserControl
        {
            private string _UrlFormat;
            private int _PageSize = 10;
            private int _RecordCount;
            private int _PageCount = 5;
     
            /**//// <summary>
            /// 连接格式
            /// </summary>
            public string UrlFormat
            {
                get
                {
                    return _UrlFormat;
                }
                set
                {
                    _UrlFormat = value;
                }
            }
     
            /**//// <summary>
            /// 页长度
     
     
            /// </summary>
            public int PageSize
            {
                get
                {
                    return _PageSize;
                }
                set
                {
                    _PageSize = value;
                }
            }
     
            /**//// <summary>
            /// 当前页码
            /// </summary>
            public int PageIndex
            {
                get
                {
                    string Pageindex = HttpContext.Current.Request.QueryString["i"];
                    if (Pageindex != null)
                    {
                        return int.Parse(Pageindex);
                    }
                    return 1;
                }
            }
     
            /**//// <summary>
            /// 总记录数
            /// </summary>
            public int RecordCount
            {
                get
                {
                    return _RecordCount;
                }
                set
                {
                    _RecordCount = value;
                }
            }
     
            /**//// <summary>
            /// 两边显示个数
            /// </summary>
            public int PageCount
            {
                get
                {
                    return _PageCount;
                }
                set
                {
                    _PageCount = value;
                }
            }
     
            protected override void Render(HtmlTextWriter writer)
            {
                if (RecordCount == 0)
                    return;
                int SumPage = (RecordCount + PageSize - 1) / PageSize;
     
                int start = PageIndex - PageCount;
                int end = PageIndex + PageCount;
     
                //以PageIndex为中心,前后个显示Page个页码导航
     
     
                if (SumPage > (PageCount * 2 + 1))
                {
                   if (start < 1)
                   {
                       start = 1;
                       end = start + 10;
                   }
                   else if (end > SumPage)
                   {
                       start = SumPage - 10;
                       end = SumPage;
                   }
               }
               else
               {
                   start = 1;
                   end = SumPage;
               }

     

               string tmp = "<a href=\"" + UrlFormat + "\">[{0}]</a>";
               StringBuilder sb = new StringBuilder(string.Format("页次:{0}/{1}  每页:{2}  共计:{3} 条 ", PageIndex, SumPage, PageSize, RecordCount));
                if (PageIndex > 1)
                {
                    sb.Append(string.Format("<a href=\"" + UrlFormat + "\">首页</a> ", 1));
                    sb.Append(string.Format(" <a href=\"" + UrlFormat + "\">上一页</a> ", PageIndex - 1));
                }
                for (int i = start; i <= end; i++)
                {
                    if (i == PageIndex)
                    {
                        sb.Append(" <strong>" + PageIndex.ToString() + "</strong> ");
                    }
                    else
                    {
                        sb.Append(string.Format(tmp, i));
                    }
                    sb.Append("&nbsp;");
                }
                if (PageIndex < SumPage)
                {
                    sb.Append(string.Format(" <a href=\"" + UrlFormat + "\">下一页</a> ", PageIndex + 1));
                    sb.Append(string.Format(" <a href=\"" + UrlFormat + "\">尾页</a>", SumPage));
                }
                writer.Write(sb.ToString());
            }
            protected void Page_Load(object sender, EventArgs e)
            {

            }
        }

    使用方法:

    把Pager拖拽到页面上,进入页面后台代码,设置如下:

    Pager1.UrlFormat = "?i={0}";//分页格式
    int recordcount, pagecount;
    Repeater1.DataSource = 数据源;
    Repeater1.DataBind();
    Pager1.RecordCount = recordcount;
  • 相关阅读:
    第9章 使用ssh服务管理远程主机。
    Linux下的网络管理工具—OpenNMS
    第8章 Iptables与Firewalld防火墙
    Linux下更好用的帮助命令—cheat
    第7章 使用RAID与LVM磁盘阵列技术
    收藏的博客
    linux下vi编辑器常用命令
    搜索引擎高级使用技巧
    七牛云配置二级域名
    软考-系统架构师备考知识(一)
  • 原文地址:https://www.cnblogs.com/jackrebel/p/1286188.html
Copyright © 2011-2022 走看看