• C#_简单实用的翻页


    简单实用的生成翻页HTML辅助类

    C#

      1 using System.Text;
      2 namespace ClassLibrary
      3 {
      4     /// <summary>
      5     /// 
      6     /// </summary>
      7     public class PageTurning
      8     {
      9         #region 构造函数
     10         /// <summary>
     11         /// 翻页
     12         /// </summary>
     13         /// <param name="count">页码总量</param>
     14         /// <param name="showCount">显示数量</param>
     15         /// <param name="index">当前页码</param>
     16         /// <param name="element">显示元素</param>
     17         /// <param name="css">默认样式</param>
     18         /// <param name="cssChecked">选中样式</param>
     19         /// <param name="cssPageTurning">上一页下一页样式</param>
     20         /// <param name="click">翻页事件</param>
     21         public PageTurning(int count, int showCount, int index, string element, string css, string cssChecked, string cssPageTurning, string click)
     22         {
     23             this.Count = count;
     24             this.ShowCount = showCount;
     25             this.Index = index;
     26             this.Element = element;
     27             this.Css = css;
     28             this.CssChecked = cssChecked;
     29             this.CssPageTurning = cssPageTurning;
     30             this.Click = click;
     31         }
     32         #endregion
     33 
     34         #region 参数
     35         #region 基本参数
     36         /// <summary>
     37         /// 当前页码
     38         /// </summary>
     39         public int Index { get; set; }
     40         /// <summary>
     41         /// 页码总量
     42         /// </summary>
     43         public int Count { get; set; }
     44         /// <summary>
     45         /// 显示数量
     46         /// </summary>
     47         public int ShowCount { get; set; }
     48         #endregion
     49 
     50         #region HTML参数
     51         /// <summary>
     52         /// 显示元素
     53         /// </summary>
     54         public string Element { get; set; }
     55         /// <summary>
     56         /// 默认样式
     57         /// </summary>
     58         public string Css { get; set; }
     59         /// <summary>
     60         /// 选中样式
     61         /// </summary>
     62         public string CssChecked { get; set; }
     63         /// <summary>
     64         /// 上一页下一页样式
     65         /// </summary>
     66         public string CssPageTurning { get; set; }
     67         /// <summary>
     68         /// 翻页事件
     69         /// </summary>
     70         public string Click { get; set; }
     71         #endregion
     72         #endregion
     73 
     74         #region 生成HTML
     75 
     76         #region 获取最大最小值
     77         /// <summary>
     78         /// 获取最大最小值
     79         /// </summary>
     80         /// <returns></returns>
     81         private int[] GetMinMax()
     82         {
     83             int[] minMax = new int[2];
     84             if (Index < ShowCount + 2)//123...
     85             {
     86                 minMax[0] = 1;
     87                 minMax[1] = Count > (ShowCount * 2 + 1) ? (ShowCount * 2 + 1) : Count;
     88             }
     89             else if (Index > Count - 2 * ShowCount + 1)//...678
     90             {
     91                 minMax[0] = Count - 2 * ShowCount;
     92                 minMax[1] = Count;
     93             }
     94             else//...345...
     95             {
     96                 minMax[0] = Index - ShowCount;
     97                 minMax[1] = Index + ShowCount;
     98             }
     99             //防止出错
    100             minMax[0] = minMax[0] < 1 ? 1 : minMax[0];
    101             minMax[1] = minMax[1] < Count ? minMax[1] : Count;
    102             minMax[1]++;
    103             return minMax;
    104         }
    105         #endregion
    106 
    107         #region 生成HTML
    108         /// <summary>
    109         /// 生成HTML
    110         /// </summary>
    111         /// <returns></returns>
    112         public string Building()
    113         {
    114             var minMax = GetMinMax();
    115             StringBuilder sb = new StringBuilder();
    116             if (Index > 1)//加1...
    117             {
    118                 sb.AppendFormat("<{0} class={1} onclick={2}>上一页</{0}>", Element, CssPageTurning, Click + "(" + (Index > 1 ? Index - 1 : 1) + ")");
    119                 if (minMax[0] > 1)
    120                 {
    121                     sb.AppendFormat("<{0} class={1} onclick={2}>1</{0}>", Element, Css, Click + "(1)");
    122                     if (minMax[0] > 2)
    123                     {
    124                         sb.AppendFormat("<{0} class={1}>...</{0}>", Element, Css);
    125                     }
    126                 }
    127             }
    128             while (minMax[0] < minMax[1])//1...345...7
    129             {
    130                 sb.AppendFormat("<{0} class={1} onclick={2}>{3}</{0}>", Element, minMax[0] != Index ? Css : CssChecked, minMax[0] != Index ? Click + "(" + minMax[0] + ")" : "", minMax[0]);
    131                 minMax[0]++;
    132             }
    133             if (Index < Count)//加..7
    134             {
    135                 if (minMax[1] < Count + 1)
    136                 {
    137                     if (minMax[1] < Count)
    138                     {
    139                         sb.AppendFormat("<{0} class={1}>...</{0}>", Element, Css);
    140                     }
    141                     sb.AppendFormat("<{0} class={1} onclick={2}>{3}</{0}>", Element, Css, Click + "(" + Count + ")", Count);
    142                 }
    143                 sb.AppendFormat("<{0} class={1} onclick={2}>下一页</{0}>", Element, CssPageTurning, Click + "(" + (Count > Index ? Index + 1 : Count) + ")");
    144             }
    145             return sb.ToString();
    146         }
    147         #endregion
    148         #endregion
    149     }
    150 }
    View Code

    调用

    public string get(int index)
    {
      PageTurning pt = new PageTurning(20,1,index,"a","aa","bb","cc","aaaaa");
      return pt.Building();
    }

    HTML实例

     1 <!DOCTYPE html>
     2 <html>
     3 <head>
     4     <meta name="viewport" content="width=device-width" />
     5     <title>Index</title>
     6     <script src="~/Scripts/jquery-1.8.2.min.js"></script>
     7     <script>
     8         $(function () {
     9             aaaaa(22)
    10         })
    11         function aaaaa(pIndex) {
    12             $("div").load("/test/get", { index: pIndex });
    13         }
    14     </script>
    15     <style>
    16         .aa {
    17             cursor: pointer;
    18             display: block;
    19             border: 1px solid black;
    20             width: 22px;
    21             height: 24px;
    22             float: left;
    23             margin-left: 4px;
    24         }
    25 
    26         .bb {
    27             cursor: pointer;
    28             display: block;
    29             border: 1px solid black;
    30             width: 22px;
    31             height: 24px;
    32             float: left;
    33             margin-left: 4px;
    34             background-color: pink;
    35         }
    36 
    37         .cc {
    38             cursor: pointer;
    39             display: block;
    40             border: 1px solid black;
    41             width: 60px;
    42             height: 24px;
    43             float: left;
    44             margin-left: 4px;
    45             background-color: orange;
    46         }
    47     </style>
    48 </head>
    49 <body>
    50     <div></div>
    51 </body>
    52 </html>
    View Code

    效果图

  • 相关阅读:
    C++自定义异常类
    上下栏固定, 中间滚动的HTML模板
    C# 代理应用
    C# 对象池的实现(能限制最大实例数量,类似于WCF的MaxInstanceCount功能)
    半同步半异步模式的实现
    EventBus实现
    C Socket初探
    C Socket初探
    VC++全屏
    MFC 坦克定位
  • 原文地址:https://www.cnblogs.com/liuph/p/4353102.html
Copyright © 2020-2023  润新知