简单实用的生成翻页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

相关文章:

  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2022-03-06
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
猜你喜欢
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
  • 2021-07-16
  • 2021-11-18
  • 2021-07-31
相关资源
相似解决方案