当前位置: 移动技术网 > IT编程>开发语言>.net > 简单好用的ASP.NET分页类(支持AJAX、自定义文字)

简单好用的ASP.NET分页类(支持AJAX、自定义文字)

2017年12月12日  | 移动技术网IT编程  | 我要评论

色网书库,恩施火车站时刻表,孕妇待产包

在做网站没用 js ui控件时 很实用

用法:

var ps=new pagestring();
 
 /*可选参数*/
 
 ps.setisenglish = true;// 是否是英文    (默认:false)
 ps.setisshowtext = true;//是否显示分页文字 (默认:true)
 //ps.textformat=""             (默认值:《span class=\"pagetext\"》《strong》总共《/strong》:{0} 条 《strong》当前《/strong》:{1}/{2}《/span》)
 //ps.setpageindexname request["pageindex"](默认值:"pageindex")
 ps.setisajax = false;//          (默认值:"false")
 
 /*函数参数*/
 int total = 10000;
 int pagesize = 10;
 int pageindex = convert.toint32(request["pageindex"]);
 
 var page = ps.tostring(total, pagesize, pageindex, "/ui/pagestringtest.aspx?");
 
 //获取 page html 输出
response.write(page);

效果:

代码:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.text.regularexpressions;
 
namespace syntacticsugar
{
  /// <summary>
  /// ** 描述:分页类
  /// ** 创始时间:2015-5-29
  /// ** 修改时间:-
  /// ** 作者:sunkaixuan
  public class pagestring
  {
    /// <summary>
    /// 是否是英文   (默认:false)
    /// </summary>
    public bool setisenglish { get; set; }
    /// <summary>
    /// 是否显示分页文字(默认:true)
    /// </summary>
    public bool setisshowtext { get; set; }
    /// <summary>
    /// 样式      (默认:"pagination")
    /// </summary>
    public string setclassname { get; set; }
    /// <summary>
    /// 分页参数名   (默认:"pageindex")
    /// </summary>
    public string setpageindexname { get; set; }
    /// <summary>
    /// 是否是异步 同步 href='' 异步 onclick=ajaxpage()  (默认:false)
    /// </summary>
    public bool setisajax { get; set; }
 
    /// <summary>
    /// 自定义文字
    /// string.format("{0}{1}{2}","总记录数","当前页数","总页数")
    /// 默认值:《span class=\"pagetext\"》《strong》总共《/strong》:{0} 条 《strong》当前《/strong》:{1}/{2}《/span》
    /// </summary>
    public string settextformat { get; set; }
 
    public pagestring()
    {
      setisenglish = false;
      setisshowtext = true;
      settextformat = "<span class=\"pagetext\"><strong>总共</strong>:{0} 条 <strong>当前</strong>:{1}/{2}</span> ";
      setclassname = "pagination";
      setpageindexname = "pageindex";
      setisajax = false;
    }
 
    /*免费的样式
    .pagination .click {cursor:pointer}
    .pagination a{text-decoration: none;border: 1px solid #aae;color: #15b;font-size: 13px;border-radius: 2px;}
    .pagination span{ color:#666;font-size:13px;display: inline-block;border: 1px solid #ccc;padding: 0.2em 0.6em;}
    .pagination span.pagetext{ border:none}
    .pagination a:hover{background: #26b;color: #fff;}
    .pagination a{display: inline-block;padding: 0.2em 0.6em;}
    .pagination .page_current{background: #26b;color: #fff;border: 1px solid #aae;margin-right: 5px;}
    .pagination{margin-top: 20px;}
    .pagination .current.prev, .pagination .current.next{color: #999;border-color: #999;background: #fff;}
     * */
 
    /// <summary>
    /// 分页算法<一>共20页 首页 上一页 1 2 3 4 5 6 7 8 9 10 下一页 末页
    /// </summary>
    /// <param name="total">总记录数</param>
    /// <param name="pagesize">每页记录数</param>
    /// <param name="pageindex">当前页数</param>
    /// <param name="query_string">url参数</param>
    /// <returns></returns>
    public string tostring(int total, int pagesize, int pageindex, string query_string)
    {
 
      int allpage = 0;
      int next = 0;
      int pre = 0;
      int startcount = 0;
      int endcount = 0;
      stringbuilder pagestr = new stringbuilder();
      pageindex = pageindex == 0 ? 1 : pageindex;
      pagestr.appendformat("<div class=\"{0}\" >", setclassname);
      if (pageindex < 1) { pageindex = 1; }
      //计算总页数
      if (pagesize != 0)
      {
        allpage = (total / pagesize);
        allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage);
        allpage = (allpage == 0 ? 1 : allpage);
      }
      next = pageindex + 1;
      pre = pageindex - 1;
      startcount = (pageindex + 5) > allpage ? allpage - 9 : pageindex - 4;//中间页起始序号
      //中间页终止序号
      endcount = pageindex < 5 ? 10 : pageindex + 5;
      if (startcount < 1) { startcount = 1; } //为了避免输出的时候产生负数,设置如果小于1就从序号1开始
      if (allpage < endcount) { endcount = allpage; }//页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内
 
      bool isfirst = pageindex == 1;
      bool islast = pageindex == allpage;
 
      if (setisshowtext)
        pagestr.appendformat(settextformat, total, pageindex, allpage);
 
      if (isfirst)
      {
        pagestr.append("<span>首页</span> <span>上一页</span>");
      }
      else
      {
        pagestr.appendformat("<a href=\"{0}pageindex=1\">首页</a> <a href=\"{0}pageindex={1}\">上一页</a>", query_string, pre);
      }
      //中间页处理,这个增加时间复杂度,减小空间复杂度
      for (int i = startcount; i <= endcount; i++)
      {
        bool iscurent = pageindex == i;
        if (iscurent)
        {
          pagestr.append(" <a class=\"page_current\">" + i + "</a>");
        }
        else
        {
          pagestr.append("  <a href=\"" + query_string + "pageindex=" + i + "\">" + i + "</a>");
        }
 
      }
      if (islast)
      {
        pagestr.append("<span>下一页</span> <span>末页</span>");
      }
      else
      {
        pagestr.append(" <a href=\"" + query_string + "pageindex=" + next + "\">下一页</a> <a href=\"" + query_string + "pageindex=" + allpage + "\">末页</a>");
      }
      pagestr.appendformat("</div>");
      return conversiondata(pagestr.tostring());
    }
 
    private string conversiondata(string page)
    {
      if (setisenglish)
      {
        page= page.replace("上一页", "previous").replace("下一页", "next").replace("总共", "total").replace("当前", "current").replace("条", "records").replace("首页", "first").replace("末页", "last");
      }
      if (setisajax)
      {
        var matches = regex.matches(page, @"href\="".*?""",regexoptions.singleline);
        if (matches != null && matches.count > 0)
        {
          foreach (match m in matches)
          {
            page = page.replace(m.value, string.format("class=\"click\" onclick=\"ajaxpage('{0}')\"", regex.match(m.value, string.format(@"{0}\=(\d+)", setpageindexname)).groups[1].value));
          }
        }
      }
      return page;
 
    }
 
  }
 
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网