zoukankan      html  css  js  c++  java
  • 小白写的一个ASP.NET分页控件,仅供娱乐

    无聊,第一次写博客,自己动手写了一个分页控件。由于我是新手,有很多地方写得不够好,希望各位大牛多多指正。哈哈哈

      1 /// <summary>
      2     /// 分页控件
      3     /// </summary>
      4     public class Pager
      5     {
      6         /// <summary>
      7         /// 每页记录行数参数名称
      8         /// </summary>
      9         public string PageSizeName { get; set; }
     10         /// <summary>
     11         /// 分页当前索引页参数名称
     12         /// </summary>
     13         public string PageIndexName { get; set; }
     14 
     15         /// <summary>
     16         /// 每页记录行数
     17         /// </summary>
     18         public int PageSize { get; private set; }
     19         /// <summary>
     20         /// 分页当前索引页
     21         /// </summary>
     22         public int PageIndex { get; private set; }
     23         /// <summary>
     24         /// 总记录行数
     25         /// </summary>
     26         public int TotalCount { get; private set; }
     27         /// <summary>
     28         /// 分页摁钮最大显示数量
     29         /// </summary>
     30         public int MaxDisplayCount { get; set; }
     31 
     32         /// <summary>
     33         /// 是否显示上一页摁钮
     34         /// </summary>
     35         public bool ShowPrevPage { get; set; }
     36         /// <summary>
     37         /// 是否显示下一页摁钮
     38         /// </summary>
     39         public bool ShowNextPage { get; set; }
     40         /// <summary>
     41         /// 是否显示第一页摁钮
     42         /// </summary>
     43         public bool ShowFirstPage { get; set; }
     44         /// <summary>
     45         /// 是否显示最后一页摁钮
     46         /// </summary>
     47         public bool ShowLastPage { get; set; }
     48 
     49         /// <summary>
     50         /// 上一页摁钮显示文本
     51         /// </summary>
     52         public string PrevPageText { get; set; }
     53         /// <summary>
     54         /// 下一页摁钮显示文本
     55         /// </summary>
     56         public string NextPageText { get; set; }
     57         /// <summary>
     58         /// 第一页摁钮显示文本
     59         /// </summary>
     60         public string FirstPageText { get; set; }
     61         /// <summary>
     62         /// 最后一页摁钮显示文本
     63         /// </summary>
     64         public string LastPageText { get; set; }
     65 
     66         /// <summary>
     67         /// 上一页摁钮样式
     68         /// </summary>
     69         public string PrevPageClass { get; set; }
     70         /// <summary>
     71         /// 下一页摁钮样式
     72         /// </summary>
     73         public string NextPageClass { get; set; }
     74         /// <summary>
     75         /// 第一页摁钮样式
     76         /// </summary>
     77         public string FirstPageClass { get; set; }
     78         /// <summary>
     79         /// 最后一页摁钮样式
     80         /// </summary>
     81         public string LastPageClass { get; set; }
     82 
     83         /// <summary>
     84         /// 普通分页摁钮样式
     85         /// </summary>
     86         public string ANormalClass { get; set; }
     87         /// <summary>
     88         /// 选中分页摁钮样式
     89         /// </summary>
     90         public string ACurrentClass { get; set; }
     91 
     92         /// <summary>
     93         /// 分页控件构造函数
     94         /// </summary>
     95         /// <param name="PageIndex">当前选中页索引</param>
     96         /// <param name="PageSize">每页显示记录行数</param>
     97         /// <param name="TotalCount">总记录行数</param>
     98         /// <param name="UrlAndQuery">分页控件回传地址</param>
     99         public Pager(int PageIndex, int PageSize, int TotalCount)
    100         {
    101             this.PageSize = PageSize;
    102             this.PageIndex = PageIndex;
    103             this.TotalCount = TotalCount;
    104             this.MaxDisplayCount = 10;
    105 
    106             this.PageSizeName = "PageSize";
    107             this.PageIndexName = "PageIndex";
    108             this.ACurrentClass = "current";
    109 
    110             this.ShowPrevPage = true;
    111             this.ShowNextPage = true;
    112             this.ShowFirstPage = true;
    113             this.ShowLastPage = true;
    114 
    115             this.PrevPageText = "<";
    116             this.NextPageText = ">";
    117             this.FirstPageText = "|<";
    118             this.LastPageText = ">|";
    119         }
    120 
    121         /// <summary>
    122         /// 生成分页控件Html代码段
    123         /// </summary>
    124         /// <returns></returns>
    125         public string GeneratePagerHtml()
    126         {
    127             // 解析Url,对分页索引键值进行特殊处理
    128             var request = HttpContext.Current.Request;
    129             var keys = request.QueryString.AllKeys;
    130             var paramList = new List<string>();
    131             if (keys.HasItem())
    132             {
    133                 foreach (var key in keys)
    134                 {
    135                     if (key == PageIndexName)
    136                     {
    137                         continue;
    138                     }
    139                     var value = HttpUtility.UrlEncode(request.QueryString[key]);
    140                     paramList.Add(string.Format("{0}={1}", key, value));
    141                 }
    142             }
    143             paramList.Add(string.Format("{0}=", PageIndexName));
    144             var query = paramList.Join("&");
    145 
    146             // 对分页摁钮显示的数量和范围做处理
    147             var PageCount = (int)Math.Ceiling((double)TotalCount / PageSize);
    148             var startPage = PageIndex - MaxDisplayCount / 2 + 1;
    149             startPage = startPage < 1 ? 1 : startPage;
    150             var endPage = startPage + MaxDisplayCount - 1;
    151             endPage = endPage > PageCount ? PageCount : endPage;
    152             startPage = endPage - MaxDisplayCount + 1;
    153             startPage = startPage < 1 ? 1 : startPage;
    154 
    155             var a = "<a href="?{0}{1}" class="{2}">{3}</a>";
    156             var aString = new StringBuilder();
    157             for (var nowPage = startPage; nowPage <= endPage; ++nowPage)
    158             {
    159                 var nowAClass = nowPage == PageIndex ? ACurrentClass : ANormalClass;
    160                 aString.AppendFormat(a, query, nowPage, nowAClass, nowPage);
    161             }
    162 
    163             // 对上一页摁钮做处理
    164             string PrevPage = null;
    165             if (ShowPrevPage)
    166             {
    167                 var PrevPageIndex = PageIndex - 1;
    168                 PrevPageIndex = PrevPageIndex < 1 ? 1 : PrevPageIndex;
    169                 PrevPage = string.Format(a, query, PrevPageIndex, PrevPageClass, PrevPageText);
    170             }
    171 
    172             // 对下一页摁钮做处理
    173             string NextPage = null;
    174             if (ShowNextPage)
    175             {
    176                 var NextPageIndex = PageIndex + 1;
    177                 NextPageIndex = NextPageIndex > PageCount ? PageCount : NextPageIndex;
    178                 NextPage = string.Format(a, query, NextPageIndex, NextPageClass, NextPageText);
    179             }
    180 
    181             // 对第一页摁钮做处理
    182             string FirstPage = null;
    183             if (ShowFirstPage)
    184             {
    185                 FirstPage = string.Format(a, query, 1, FirstPageClass, FirstPageText);
    186             }
    187 
    188             // 对最后一页摁钮做处理
    189             string LastPage = null;
    190             if (ShowLastPage)
    191             {
    192                 LastPage = string.Format(a, query, PageCount, LastPageClass, LastPageText);
    193             }
    194 
    195             // 分页控件Html代码段
    196             var htmlTemp = "<div>{0}{1}{2}{3}{4}</div><div>第 {5}/{6} 页,{7} 条/页,共 {8} 条记录</div>";
    197             var PagerHtml = string.Format(htmlTemp, FirstPage, PrevPage, aString, NextPage, LastPage, PageIndex, PageCount, PageSize, TotalCount);
    198 
    199             return PagerHtml;
    200         }
    201     }
    View Code

    Default.aspx.cs:

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 using System.Web.UI;
     6 using System.Web.UI.WebControls;
     7 using AspNetPager;
     8 
     9 namespace Test5
    10 {
    11     public partial class _Default : System.Web.UI.Page
    12     {
    13         public int PageIndex
    14         {
    15             get 
    16             {
    17                 var pageIndex = 1;
    18                 try
    19                 {
    20                     pageIndex = Convert.ToInt32(Request.QueryString["PageIndex"].ToString());
    21                 }
    22                 catch { }
    23                 return pageIndex;
    24             }
    25         }
    26 
    27         public int PageSize
    28         {
    29             get
    30             {
    31                 var PageSize = 8;
    32                 try
    33                 {
    34                     PageSize = Convert.ToInt32(Request.QueryString["PageSize"].ToString());
    35                 }
    36                 catch { }
    37                 return PageSize;
    38             }
    39         }
    40 
    41         protected void Page_Load(object sender, EventArgs e)
    42         {
    43             var pager = new Pager(PageIndex, PageSize, 100);
    44             this.Pager.InnerHtml = pager.CeneratePagerHtml();
    45         }
    46     }
    47 }
    View Code

    Default.aspx:

     1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test5._Default" %>
     2 
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4 <html xmlns="http://www.w3.org/1999/xhtml">
     5 <head runat="server">
     6     <title></title>
     7     <style type="text/css">
     8         .pager a
     9         {
    10             color: Blue;
    11             width: 40px;
    12             height: 25px;
    13             border: 1px solid #CCC;
    14             margin: 0px 5px;
    15             text-align: center;
    16             line-height: 25px;
    17             vertical-align: middle;
    18             text-decoration: none;
    19             display: inline-block;
    20         }
    21         .pager a:hover, .pager a.current
    22         {
    23             background-color: #555;
    24             color: White;
    25         }
    26     </style>
    27 </head>
    28 <body>
    29     <form id="form1" runat="server">
    30     <div id="Pager" runat="server" class="pager">
    31     </div>
    32     </form>
    33 </body>
    34 </html>
    View Code
  • 相关阅读:
    Table的基本操作
    MySQL数据库基本操作
    jmeter中服务器返回的cookies的查看
    jemeter的乱码问题
    cucumber的报告
    Cucumber的依赖
    idea里maven执行插件pom文件依赖设置
    Tomcat和jenkins的安装
    maven配置
    Ajax必知必会
  • 原文地址:https://www.cnblogs.com/Jarvan-Chan/p/5341123.html
Copyright © 2011-2022 走看看