• 简单实用的分页类


    一、写一个分页类
    using System;
    using System.Text;
    using System.Diagnostics;
    namespace Zhuwenfeng
    {
        public class MyPager
        {
            /// <summary>
            /// 总数据条数
            /// </summary>
            public int TotalCount { get; set; }
            /// <summary>
            /// 每页数据条数
            /// </summary>
            public int PageSize { get; set; }
            /// <summary>
            /// 当前页码(从1开始)
            /// </summary>
            public int CurrentPageIndex { get; set; }
            /// <summary>
            /// 显示出来最多的页码数量,因为假设有100页,不可能把100页都显示到界面上
            /// </summary>
            public int MaxPagerCount { get; set; }
            /// <summary>
            /// 页码链接的地址格式,页码用{n}占位。
            /// </summary>
            public string UrlFormat { get; set; }
            public MyPager()
            {
                PageSize = 10;
                MaxPagerCount = 10;
            }
            /// <summary>
            /// 尝试从字符串pn中解析当前页面赋值给CurrentPageIndex
            /// </summary>
            /// <param name="pn"></param>
            public void TryParseCurrentPageIndex(string pn)
            {
                int temp;
                if (int.TryParse(pn, out temp))
                {
                    CurrentPageIndex = temp;
                }
                else
                {
                    CurrentPageIndex = 1;
                }
            }
            /// <summary>
            /// 创建页码链接
            /// </summary>
            /// <param name="i">页码</param>
            /// <param name="text">链接文本</param>
            /// <returns></returns>
            private string GetPageLink(int i,string text)
            {
                StringBuilder sb = new StringBuilder();
                string url = UrlFormat.Replace("{n}", i.ToString());
                sb.Append("<a href='").Append(url).Append("'>").Append(text).Append("</a>");
                return sb.ToString();
            }
            /// <summary>
            /// 检查输入参数
            /// </summary>
            private void Check()
            {
                Debug.Assert(PageSize>0);
                Debug.Assert(CurrentPageIndex >= 0);
                Debug.Assert(!string.IsNullOrEmpty(UrlFormat));
            }
            /// <summary>
            ///渲染到前台到得HTML代码
            /// </summary>
            /// <returns></returns>
            public string Render()
            {
                Check();
                StringBuilder sb = new StringBuilder();
                double tempCount = TotalCount / PageSize;
                int pageCount = (int)Math.Ceiling(tempCount);
                //计算显示的页码数(当总页码大于MaxPagerCount)的起始页码
                int visibleStart = CurrentPageIndex-MaxPagerCount/2;
                if (visibleStart <1)
                {
                    visibleStart = 1;
                }
                //计算显示的页码数(当总页码大于MaxPagerCount)的起始页码
                int visibleEnd = visibleStart + MaxPagerCount;
                //显示最多MaxPagerCount条
                //如果算出来的结束页码大于总页码的话则调整为最大页码
                if (visibleEnd >pageCount)
                {
                    visibleEnd = pageCount;
                }
                if (CurrentPageIndex > 1)
                {
                    sb.Append(GetPageLink(1, "首页"));
                    sb.Append(GetPageLink(CurrentPageIndex - 1, "上一页"));
                }
                else
                {
                    sb.Append("<span>首页</span>");
                    //如果没有上一页了,则只显示一个上一页的文字,没有超链接
                    sb.Append("<span>上一页</span>");
                }
                //绘制可视的页码链接
                for (int i = visibleStart; i <= visibleEnd; i++)
                {
                    //当前页不是超链接
                    if (i == CurrentPageIndex)
                    {
                        sb.Append("<span>").Append(i).Append("</span>");
                    }
                    else
                    {
                        sb.Append(GetPageLink(i,i.ToString()));
                    }               
                }
                if (CurrentPageIndex < pageCount)
                {
                    sb.Append(GetPageLink(CurrentPageIndex + 1, "下一页"));
                    sb.Append(GetPageLink(pageCount + 1, "末页"));
                }
                else
                {
                    sb.Append("<span>下一页</span>");
                    sb.Append("<span>末页</span>");
                }           
                return sb.ToString();
            }
        }
    }
     
    二、UI层代码示例
    public partial class 分页测试 : System.Web.UI.Page
        {
            protected string PagerHTML { get; private set; }
            protected void Page_Load(object sender, EventArgs e)
            {
                var pager = new MyPager();
                pager.UrlFormat = "分页测试.aspx?pagenum={n}";
                pager.PageSize = 10;
                pager.TryParseCurrentPageIndex(Request["pagenum"]);
                int startRowIndex = (pager.CurrentPageIndex - 1) * pager.PageSize;
                so_KeywordLogBLL bll = new so_KeywordLogBLL();
                pager.TotalCount = bll.GetTotalCount();
                Repeater1.DataSource = bll.GetPagedData(startRowIndex, startRowIndex + pager.PageSize - 1);
                Repeater1.DataBind();
                PagerHTML = pager.Render();//渲染页码条HTML
            }
        }
     
    三、前台代码
        <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                    <li>
                        <%#Eval("Id") %>|<%#Eval("KeyWord") %>
                    </li>
                </ItemTemplate>
            </asp:Repeater>
            <div class="pager">
                <%=PagerHTML%>
            </div>
        </div>
        </form>
     
    四、加一个CSS更好看
     <style type="text/css">
            .pager
            {
                text-align: center;
                padding-bottom: 3px;
                padding-left: 0px;
                padding-right: 0px;
                float: right;
                padding-top: 3px;
            }
            .pager a
            {
                border-bottom: #ccc 1px solid;
                text-align: left;
                border-left: #ccc 1px solid;
                padding-bottom: 3px;
                line-height: 26px;
                margin: 0px 2px;
                outline-style: none;
                padding-left: 5px;
                padding-right: 5px;
                background: #fff;
                color: #000;
                font-size: 12px;
                border-top: #ccc 1px solid;
                border-right: #ccc 1px solid;
                text-decoration: none;
                padding-top: 4px;
            }
            .pager a:hover
            {
                border-bottom: #f80 1px solid;
                border-left: #f80 1px solid;
                color: #f80;
                border-top: #f80 1px solid;
                border-right: #f80 1px solid;
                text-decoration: underline;
            }
            .pager a:focus
            {
                -moz-outline-style: none;
            }
            .pager span
            {
                border-bottom-style: none;
                text-align: left;
                padding-bottom: 4px;
                line-height: 26px;
                border-right-style: none;
                margin: 1px 2px;
                padding-left: 6px;
                padding-right: 6px;
                border-top-style: none;
                background: #f80;
                color: #fff;
                font-size: 12px;
                border-left-style: none;
                padding-top: 5px;
            }
        </style>
     
    五、再加一个数据访问层的Select代码
     public int GetTotalCount()
            {
                string sql = "SELECT count(*) FROM so_KeywordLog";
                return (int)DbHelperSQL.GetSingle(sql);
            }
            public DataSet GetPagedData(int minrownum, int maxrownum)
            {
                string sql = "SELECT * from(SELECT *,(row_number() over(order by Id))-1 rownum FROM so_KeywordLog) t where rownum>=@minrownum and rownum<=@maxrownum";
                SqlParameter[] parameters = { new SqlParameter("@minrownum", SqlDbType.Int, 4), new SqlParameter("@maxrownum", SqlDbType.Int, 4) };
                parameters[0].Value = minrownum;
                parameters[1].Value = maxrownum;
                return  DbHelperSQL.Query(sql, parameters);
            }
     
    六、相关分页Select语句(扩展)
    1 存储过程
    create PROCEDURE GetPageDataOutRowPageCount2
    (
    @PageIndex int = 1,--当前页数
    @PageSize  int = 10,--每页大小
    @RowCount int output,--总行数(传出参数)
    @PageCount int output--总页数(传出参数)
    )
    AS
    begin
     DECLARE @sql NVARCHAR(225),@sqlCount NVARCHAR(225)
     select @RowCount =COUNT(SID),@PageCount=CEILING((COUNT(SID)+0.0)/@PageSize) FROM Students
     SET @sql='SELECT TOP '+str(@PageSize) +'  FROM Students where SID not in(select top '+str((@PageIndex-1)*@PageSize) +' SID from Students)'
     EXEC(@sql)
    end
    2 Select语句
    select top " + pageSize + " * from Students where SID not in (select top " + (pageIndex - 1) * int.Parse(pageSize) + " SID from Students)
    3 Select语句
    select * from (select * ,Row_Number()over(order by id) RowNumber from books) t
    where t.RowNumber>=11 and t.RowNumber<=20
    第n页: (PageIndex-1)*pageSize+1,PageIndex*pageSize
    4 ListView 提供的分页不说了
  • 相关阅读:
    js导出table到excel,同时兼容FF和IE
    Android 程式开发:(廿一)消息传递 —— 21.3 使用Intent发送短信
    [置顶] 让金融互联网-P2P网贷融资量增长10倍的广告宣传公益活动
    Headroom.js插件用法
    HDU 5289 Assignment(单调队列)
    sh_Spring整合Hibernate
    怎样高速启动Android模拟器(Android Emulator)
    设计模式——介绍与工厂模式(扁平管理模式VS职业经理人模式)
    在c++11中你最惊讶的新feature是什么?
    python的私有化
  • 原文地址:https://www.cnblogs.com/tianxiang2046/p/2480783.html
Copyright © 2020-2023  润新知