一个写好的分页字符串的类

  这是写好的一个分页字符串的类,直接调用,生成分页字符串后之后显示到前台页面即可

其中GetList()是我在前台页面自己写的js方法,用于从后台取分页数据。第一个参数是pageindex,第二个参数是业务需求,需要写序号,可根据自己需要忽略此参数(将此参数去掉就行)。

1
public class PagerHelper 2 { 3 public static string GetNumericPage(int pageIndex,int pageCount) 4 { 5 StringBuilder sb = new StringBuilder(); 6 if (pageIndex != 1) 7 { 8 sb.Append("<a href='javascript:GetList(1,0)'>首页</a>"); 9 sb.Append("<a href='javascript:GetList(" + (pageIndex - 1) + "," + (pageIndex - 1 - 1) * 10 + ")'>上一页</a>"); 10 }//1 2 3 4 5 11 else 12 { 13 sb.Append("<button disabled='disabled' >首页</button>"); 14 sb.Append("<button value='上一页' disabled='disabled' >上一页</button>"); 15 16 } 17 18 if (pageCount <= 5) 19 { 20 for (int i = 1; i <= pageCount; i++) 21 { 22 //如果数字是与当前页索引相同,则不加超链接 23 if (pageIndex == i) 24 { 25 sb.Append(i + " "); 26 } 27 else 28 { 29 sb.Append("<a href='javascript:GetList(" + i + "," + (i - 1) * 10 + ")'>" + i + "</a> "); 30 } 31 } 32 } 33 else//总页面数大于5时,走下面的逻辑 34 { 35 //1 ... 4 5 6 ... 10 36 37 //特例:1,pc 38 if (pageIndex == 1)//第1页 39 { 40 sb.Append("1 "); 41 for (int i = 2; i <= 4; i++) 42 { 43 sb.Append("<a href='javascript:GetList(" + i + "," + (i-1) * 10 + ")'>" + i + "</a> "); 44 } 45 sb.Append("... <a href='javascript:GetList(" + pageCount + "," + (pageCount - 1) * 10 + ")'>" + pageCount + "</a>"); 46 } 47 else if (pageIndex == pageCount)//最后一页 48 { 49 sb.Append("<a href='javascript:GetList(1,0)'>1</a> ... "); 50 for (int i = pageCount - 3; i < pageCount; i++) 51 { 52 sb.Append("<a href='javascript:GetList(" + i + "," + (i - 1) * 10 + ")'>" + i + "</a> "); 53 } 54 sb.Append(pageCount); 55 } 56 else if (pageIndex == 2) 57 { 58 //1 2 3 4 ...10 59 sb.Append("<a href='javascript:GetList(1,0)'>1</a> "); 60 sb.Append(pageIndex + " "); 61 sb.Append("<a href='javascript:GetList(3," + (3 - 1) * 10 + ")'>3</a> "); 62 sb.Append("<a href='javascript:GetList(4," + (4 - 1) * 10 + ")'>4</a> "); 63 sb.Append("... <a href='javascript:GetList(" + pageCount + "," + (pageCount - 1) * 10 + ")'>" + pageCount + "</a>"); 64 } 65 else if (pageIndex == 3) 66 { 67 //1 2 3 4 ...10 68 sb.Append("<a href='javascript:GetList(1,0)'>1</a> "); 69 sb.Append("<a href='javascript:GetList(" + 2 + "," + (2 - 1) * 10 + ")'>2</a> "); 70 sb.Append(pageIndex + " "); 71 sb.Append("<a href='javascript:GetList(" + 4 + "," + (4 - 1) * 10 + ")'>4</a> "); 72 sb.Append("... <a href='javascript:GetList(" + pageCount + "," + (pageCount - 1) * 10 + ")'>" + pageCount + "</a>"); 73 } 74 else if (pageIndex == (pageCount - 1)) 75 { 76 sb.Append("<a href='javascript:GetList(1,0)'>1</a> ..."); 77 sb.Append("<a href='javascript:GetList(" + (pageIndex-2) + "," + (pageIndex - 2 - 1) * 10 + ")'>" + (pageIndex - 2) + "</a> "); 78 sb.Append(" <a href='javascript:GetList(" + (pageIndex - 1) + "," + (pageIndex - 1 - 1) * 10 + ")'>" + (pageIndex - 1) + "</a> "); 79 sb.Append(pageIndex + " "); 80 sb.Append(" <a href='javascript:GetList(" + pageCount + "," + (pageCount - 1) * 10 + ")'>" + pageCount + "</a>"); 81 82 } 83 else if (pageIndex == (pageCount - 2)) 84 { 85 sb.Append("<a href='javascript:GetList(1,0)'>1</a> ..."); 86 sb.Append("<a href='javascript:GetList(" + (pageIndex - 1) + "," + (pageIndex - 1 - 1) * 10 + ")'>" + (pageIndex - 1) + "</a> "); 87 sb.Append(pageIndex + " "); 88 sb.Append(" <a href='javascript:GetList(" + (pageIndex + 1) + "," + (pageIndex + 1 - 1) * 10 + ")'>" + (pageIndex + 1) + "</a> "); 89 sb.Append(" <a href='javascript:GetList(" + pageCount + "," + (pageCount - 1) * 10 + ")'>" + pageCount + "</a>"); 90 } 91 else 92 { 93 sb.Append("<a href='javascript:GetList(1,0)'>1</a> ..."); 94 sb.Append("<a href='javascript:GetList(" + (pageIndex - 1) + "," + (pageIndex - 1 - 1) * 10 + ")'>" + (pageIndex - 1) + "</a> "); 95 sb.Append(pageIndex + " "); 96 sb.Append("<a href='javascript:GetList(" + (pageIndex + 1) + "," + (pageIndex + 1 - 1) * 10 + ")'>" + (pageIndex + 1) + "</a> "); 97 sb.Append("... <a href='javascript:GetList(" + pageCount + "," + (pageCount - 1) * 10 + ")'>" + pageCount + "</a>"); 98 } 99 } 100 if (pageIndex != pageCount) 101 { 102 sb.Append("<a href='javascript:GetList(" + (pageIndex + 1) + "," + (pageIndex + 1 - 1) * 10 + ")'>下一页</a>"); 103 sb.Append("<a href='javascript:GetList(" + pageCount + "," + (pageCount - 1) * 10 + ")'>尾页</a>"); 104 } 105 if (pageIndex == pageCount) 106 { 107 sb.Append("<button disabled='disabled' >下一页</button>"); 108 sb.Append("<button disabled='disabled' >尾页</button>"); 109 110 } 111 sb.Append(" 当前第"+pageIndex+"页 共"+pageCount+""); 112 sb.Append(" &nbsp;&nbsp;&nbsp;<a href='javascript:Jump();'>跳转至</a>"); 113 sb.Append("<input type='text' id='inputpage' style='width:20px;height:17px;'/>&nbsp;页"); 114 return sb.ToString(); 115 } 116 }
最后的这个Jump()方法也是在前台自己写的js方法,用于获取文本框中的数据然后调用GetList()方法进行跳转分页。
需要给传到页面的这个分页字符串引入一个Css样式文件 <p class="page_nav">
样式文件PageBar.css代码如下:
body {
}
.page_nav{clear:both; padding:15px 0; color:#666; font:normal 12px/24px Arial; text-align:center;}
.page_nav a{display:inline-block; height:22px; margin:0 2px; padding:0 8px; text-decoration:none; border:solid 1px #dbe5ee; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#fff; color:#333; font:normal 12px/22px Arial, Helvetica, sans-serif; cursor:pointer;}
.page_nav strong{display:inline-block; height:24px; margin:0 3px; padding:0 8px; border:none; -moz-border-radius:2px; -webkit-border-radius:2px; -khtml-border-radius:2px; border-radius:2px; background:#C00; color:#fff; font-weight:normal; line-height:24px; text-decoration:none;}
.page_nav a:hover,
.page_nav a.on{height:24px; margin:0 3px; border:none; background:#C00; color:#fff; line-height:24px; text-decoration:none;}
.page_nav a.select{cursor:default;}
.page_nav .view_all{display:block; text-align:center;}
.page_nav .view_all a{height:auto; margin:0; padding:0; border:none; color:#06c; line-height:24px;}
.page_nav .view_all a:hover{height:auto; margin:0; padding:0; background:none;}

效果图:

 

一个写好的分页字符串的类_第1张图片

 

你可能感兴趣的:(字符串)