• 类似于百度分页效果


    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;
  • 相关阅读:
    JS DataURL 整理(一)
    JavaScript 与 ECMAScript 的关系
    HTML5 多媒体之<svg>标签 使用
    HTML5 多媒体之<canvas>标签 使用
    [转]信号量---进程间通信 ---php版
    [转]使用PHP的ftok()函数实现基于linux下系统级进程间消息通信demo(消息队列模式)
    【转】php ftok 使用
    [转]Golang调度模型
    【转】如何分析golang程序的内存使用情况---很有用
    【转】Go 中对栈中函数进行内联---这篇讲的不如上一篇,也还可以吧。。
  • 原文地址:https://www.cnblogs.com/lx0551/p/3103020.html
Copyright © 2020-2023  润新知