惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 小小点

使用 C# 将数字转换成大写人民币 计划+日程表+严格执行 今天开始做事 享受过程 明天失踪一天 加班加班 JS模拟类实现 不能连接MySQL数据库的解决办法 解决安装 MSSQL 提示有挂起的安装问题 [转载] ASP中无组件上传文件 PHP读MYSQL中文乱码的解决方法 自己定义的模仿窗体标题栏的控件 意料之中 DHTML文件访问本地数据库[转] 在b/s开发中经常用到的javaScript技术 [转贴] 关机脚本(转载) 离开... 在MSSQL中利用存储过程进行分页查询 压缩MSSQL数据库日志文件大小
发个自己写的分页类
小小点 · 2008-05-09 · via 博客园 - 小小点

平时大多数时间都是在园子看看别人的文章,看着各路大虾挥笔自如,心中敬佩,今天也把自己手头上正在做项目里使用的分页发出来给大家评评,不要见笑啊。 

我不喜欢使用控件,所以这个分页控件没有封装成Asp.Net控件,如果哪位兄弟有兴趣,可以加工一下。

// Created By 小点 at 2008-5-3
// QQ:28043566  Email:whhq84#163.com(发邮件时请换#为@)
// 大家可随意转载,更欢迎与我交流!

using System;
using System.Web;

namespace ERP.Common
{
 /// <summary>
 /// 分页类,自动获取会获取当前地址里的参数
 /// </summary>
 public class Pager
 {
  private int ipage = 1,ipagesize = 20;
    private int prepage, nextpage;
  private int irecordcount = 0,ipagecount = 0;
  private string scriptfile = "",querystring = "";

    /// <summary>
    /// 页码
    /// </summary>
  public int Page
  {
   get {return ipage;}
   set {ipage = value;}
  }

    /// <summary>
    /// 页大小
    /// </summary>
  public int PageSize
  {
   get {return ipagesize;}
   set {ipagesize = value;}
  }

    /// <summary>
    /// 记录总数
    /// </summary>
  public int RecordCount
  {
   get {return irecordcount;}
      set
      {
        irecordcount = value;
        Calculate();
      }
  }

    /// <summary>
    /// 页总数
    /// </summary>
  public int PageCount
  {
   get {return (ipagecount>0) ? ipagecount : 1;}
   set {ipagecount = value;}
  }

    /// <summary>
    /// 当前脚本
    /// </summary>
  public string ScriptFile
  {
      get { return scriptfile; }
   set { scriptfile = value;}
  }
    /// <summary>
    /// 计算当前分页数据
    /// </summary>
  private void Calculate()
  {
      ipagecount = (irecordcount % ipagesize > 0) ? (1 + irecordcount / ipagesize) : irecordcount / ipagesize;
      //System.Web.HttpContext.Current.Response.Write(ipagecount + " / " + ipage + " / " + nextpage + "<br/>");
      ipage = (ipage > ipagecount) ? ipagecount : ipage;
      ipage = (ipage < 1) ? 1 : ipage;
      prepage = ((ipage-1) < 1) ? 1 : ipage-1;
      nextpage = ((ipage + 1) > ipagecount) ? ipagecount : ipage + 1;
      //System.Web.HttpContext.Current.Response.Write(ipagecount + " / " + ipage + " / " + nextpage + "<br/>");
     
      string QueryString = System.Web.HttpContext.Current.Request.Url.ToString();
      string[] sArr1 = QueryString.Split('?');
      if (sArr1.Length == 2)
      {
        scriptfile = sArr1[0].ToString();
        string[] sArr = sArr1[1].ToString().Split('&');
        foreach (string s in sArr)
        {
          querystring = (querystring.Length > 3 && s.Length > 0 && !querystring.EndsWith("&")) ? (querystring + "&") : querystring;         
          if (s.ToLower().IndexOf("page=") < 0)
          {
            querystring += s;
          }
        }
      }
      else
      {
        scriptfile = HttpContext.Current.Request.Url.ToString();
      }

      if (querystring.Length > 0)
      {
        scriptfile += "?" + (querystring.EndsWith("&") ? querystring : querystring + "&");
      }
      else
      {
        scriptfile += "?";
      }
  }
    /// <summary>
    /// 返回分页字符串
    /// </summary>
    /// <returns></returns>
    public string PagerStr()
    {
      //Calculate();

      string str1 = "<a href=\"" + ScriptFile + "page=1\">首页</a>&nbsp;&nbsp;";
      str1 += "<a href=\"" + ScriptFile + "page=" + prepage + "\">上一页</a>&nbsp;&nbsp;";

      str1 += "<a href=\"" + ScriptFile + "page=" + nextpage + "\">下一页</a>&nbsp;&nbsp;";
      str1 += "<a href=\"" + ScriptFile + "page=" + ipagecount + "\">末页</a>&nbsp;&nbsp;&nbsp;&nbsp;";
      str1 += "转到: <input type=\"text\" name=\"page\" id=\"page\" value=\"" + ipage + "\" class=\"pagerv\" />&nbsp;&nbsp;";
      str1 += "<input type=\"button\" name=\"btnpage\" id=\"btnpage\" value=\"GO\" class=\"btnpage\" onclick=\"javascript:location.href='" + ScriptFile + "page='+$('page').value;\"/>";

      string str2 = "记录: " + ipagesize + "/" + irecordcount + "&nbsp;&nbsp;";
      str2 += "页数: " + ipage + "/" + ipagecount + "&nbsp;&nbsp;";

    
      string str = "<div style=\"float:left;width:48%;text-align:left;\">" + str2 + "</div>";
      str += "<div style=\"float:left;width:50%;text-align:right;\">" + str1 + "</div>";
     
      return str;
    }

    public Pager(int page, int pagesize,int recordcount)
  {
      this.ipage = page;
      this.ipagesize = pagesize;
      this.irecordcount = recordcount;
    }
   
    public Pager(int page, int pagesize)
    {
      this.ipage = page;
      this.ipagesize = pagesize;
    }

    public Pager()
    { }
 }
}

// 使用示例
int iPage=1,iPageSize=10;

protected Pager pager; 
// 获取当前页码
String _page = Request.QueryString["page"].ToString();
iPage = (Text.IsNumeric(_page) && int.Parse(_page) > 0) ? int.Parse(_page) : 1;
pager = new Pager(iPage, iPageSize);pager.RecordCount = BLL.CustomerBLL.GetCustomersCount(); // 统计当前分页数据数
iPage = (iPage > pager.PageCount) ? pager.PageCount : iPage;
iPage = ("max" == _page.ToLower()) ? pager.PageCount : iPage;
     
traders = BLL.CustomerBLL.GetCustomersList(iPage, iPageSize); // 获取当前分页数据
Response.Write(pager.PagerStr());

承蒙各位赐教啦!