发个自己写的分页类

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

我不喜欢使用控件,所以这个分页控件没有封装成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());

承蒙各位赐教啦!




posted @ 2008-05-09 14:59  小小点  阅读(457)  评论(1编辑  收藏  举报