简单实用的生成翻页HTML辅助类
C#
1 using System.Text; 2 namespace ClassLibrary 3 { 4 ///5 /// 6 /// 7 public class PageTurning 8 { 9 #region 构造函数 10 ///11 /// 翻页 12 /// 13 /// 页码总量 14 /// 显示数量 15 /// 当前页码 16 /// 显示元素 17 /// 默认样式 18 /// 选中样式 19 /// 上一页下一页样式 20 /// 翻页事件 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 ///37 /// 当前页码 38 /// 39 public int Index { get; set; } 40 ///41 /// 页码总量 42 /// 43 public int Count { get; set; } 44 ///45 /// 显示数量 46 /// 47 public int ShowCount { get; set; } 48 #endregion 49 50 #region HTML参数 51 ///52 /// 显示元素 53 /// 54 public string Element { get; set; } 55 ///56 /// 默认样式 57 /// 58 public string Css { get; set; } 59 ///60 /// 选中样式 61 /// 62 public string CssChecked { get; set; } 63 ///64 /// 上一页下一页样式 65 /// 66 public string CssPageTurning { get; set; } 67 ///68 /// 翻页事件 69 /// 70 public string Click { get; set; } 71 #endregion 72 #endregion 73 74 #region 生成HTML 75 76 #region 获取最大最小值 77 ///78 /// 获取最大最小值 79 /// 80 ///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 #endregion106 107 #region 生成HTML108 /// 109 /// 生成HTML110 /// 111 ///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} οnclick={2}>上一页 ", Element, CssPageTurning, Click + "(" + (Index > 1 ? Index - 1 : 1) + ")");119 if (minMax[0] > 1)120 {121 sb.AppendFormat("<{0} class={1} οnclick={2}>1 ", Element, Css, Click + "(1)");122 if (minMax[0] > 2)123 {124 sb.AppendFormat("<{0} class={1}>... ", Element, Css);125 }126 }127 }128 while (minMax[0] < minMax[1])//1...345...7129 {130 sb.AppendFormat("<{0} class={1} οnclick={2}>{3} ", Element, minMax[0] != Index ? Css : CssChecked, minMax[0] != Index ? Click + "(" + minMax[0] + ")" : "", minMax[0]);131 minMax[0]++;132 }133 if (Index < Count)//加..7134 {135 if (minMax[1] < Count + 1)136 {137 if (minMax[1] < Count)138 {139 sb.AppendFormat("<{0} class={1}>... ", Element, Css);140 }141 sb.AppendFormat("<{0} class={1} οnclick={2}>{3} ", Element, Css, Click + "(" + Count + ")", Count);142 }143 sb.AppendFormat("<{0} class={1} οnclick={2}>下一页 ", Element, CssPageTurning, Click + "(" + (Count > Index ? Index + 1 : Count) + ")");144 }145 return sb.ToString();146 }147 #endregion148 #endregion149 }150 }
调用
public string get(int index)
{ PageTurning pt = new PageTurning(20,1,index,"a","aa","bb","cc","aaaaa"); return pt.Building(); }HTML实例
1 2 3 4 5Index 6 7 15 48 49 50 51 52
效果图