Asp.net+JS 分页
1 function pagestart() {//初始化页面,获取公司新闻 2 $("#pagediv").hide(); 3 $("#CurrentPage").val("0"); 4 var pagesize = $("#pagesize").val(); 5 var requesttye = $("#requesttype").val(); 6 var par = '{"pageIndex":"0","pageSize":"' + pagesize + '","classid":"' + $("#classid").val() + '","requesttye":"' + requesttye + '"}'; 7 getdata(par); 8 } 9 function getUrlParam(name) {//获取地址栏参数 10 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象 11 var r = window.location.search.substr(1).match(reg); //匹配目标参数 12 if (r != null) return unescape(r[2]); return null; //返回参数值 13 } 14 function getlist(th, classid) {//不同分类列表内容获取 15 $("#requesttype").val("list"); 16 var currentclassid = $("#classid").val(); 17 if (currentclassid != classid) { 18 $("#classid").val(classid); 19 $("#CurrentPage").val("0"); 20 } 21 var pagesize = $("#pagesize").val(); 22 $(th).addClass("checked").siblings().removeClass("checked"); 23 var requesttye = $("#requesttype").val(); 24 var par = '{"pageIndex":"' + $("#CurrentPage").val() + '","pageSize":"' + pagesize + '","classid":"' + classid + '","requesttye":"' + requesttye + '"}'; 25 getdata(par); 26 } 27 function getdata(par) {//根据类型,获取分页列表或单条内容 28 var requesttye = $("#requesttype").val(); 29 $.post("/HandlePage/PostHandler.ashx", { "type": requesttye, "par": par }, function (data) { 30 var newfull = $.parseJSON(data); 31 if (newfull.pageCount != "0") { 32 if (requesttye == "getcontent") { 33 $("#newcontent").html(newfull.content); 34 $("#newcontent").show(); 35 $("#newsListShow").hide(); 36 $("#pagediv").hide(); 37 } else { 38 PageControl(newfull.pageCount); 39 $("#newsListShow").html(newfull.content); 40 $("#newsListShow").show(); 41 $("#newcontent").hide(); 42 $("#pagediv").show(); 43 } 44 } else { 45 $("#newsListShow").html(""); 46 $("#pagediv").hide(); 47 $("#newcontent").hide(); 48 } 49 }); 50 } 51 function PageControl(datacount) {//分页按钮显示隐藏控制 52 var currentpage = $("#CurrentPage").val(); 53 var pagesize = $("#pagesize").val(); 54 var pagecount = Math.ceil(datacount / pagesize); 55 $("#pagecount").val(pagecount); 56 PageNumberControl(pagecount); 57 } 58 59 function PageNumberControl(pagecount) {//控制按钮上显示的页码数字 60 var ButtonArray = ["pageone", "pagetwo", "pagethree", "pagefour", "pagefive"]; 61 var CurrentPageNumber = $("#CurrentPage").val(); //查询页码 62 var RealPageNumber = parseInt(CurrentPageNumber) + 1; //看到的页码 63 if (pagecount > 5) {//总页数大于5页时,控制页号 64 if (RealPageNumber > 3) { 65 if (RealPageNumber == (parseInt(pagecount) - 2)) { 66 $("#pageone").text(parseInt(RealPageNumber) - 2); 67 $("#pagetwo").text(parseInt(RealPageNumber) - 1); 68 $("#pagethree").text(RealPageNumber); 69 $("#pagefour").text(parseInt(RealPageNumber) + 1); 70 $("#pagefive").text(parseInt(RealPageNumber) + 2); 71 } else if (RealPageNumber == (parseInt(pagecount) - 1)) { 72 $("#pageone").text(parseInt(RealPageNumber) - 2); 73 $("#pagetwo").text(parseInt(RealPageNumber) - 2); 74 $("#pagethree").text(parseInt(RealPageNumber) - 1); 75 $("#pagefour").text(RealPageNumber); 76 $("#pagefive").text(parseInt(RealPageNumber) + 1); 77 } 78 else if (RealPageNumber == pagecount) { 79 $("#pageone").text(parseInt(RealPageNumber) - 4); 80 $("#pagetwo").text(parseInt(RealPageNumber) - 3); 81 $("#pagethree").text(parseInt(RealPageNumber) - 2); 82 $("#pagefour").text(parseInt(RealPageNumber) - 1); 83 $("#pagefive").text(RealPageNumber); 84 } else if (RealPageNumber <= (parseInt(pagecount) - 3)) { 85 $("#pageone").text(parseInt(RealPageNumber) - 2); 86 $("#pagetwo").text(parseInt(RealPageNumber) - 1); 87 $("#pagethree").text(RealPageNumber); 88 $("#pagefour").text(parseInt(RealPageNumber) + 1); 89 $("#pagefive").text(parseInt(RealPageNumber) + 2); 90 } 91 } 92 } 93 else {//总页数少于6页时,控制页号 94 for (var pn = 0; pn < 5; pn++) { 95 $("#" + ButtonArray[pn]).text(parseInt(pn) + 1); 96 } 97 } 98 PageMoreButtonControl(RealPageNumber, pagecount);//更多提示控制 99 PageButtonHideOrShow(RealPageNumber,pagecount);//页码显示按键控制 100 } 101 function PageButtonHideOrShow(RealPageNumber,PageCount) {//控制页码按键和样式 102 var ButtonArray = ["pageone", "pagetwo", "pagethree", "pagefour", "pagefive"]; 103 104 for (var j = 0; j < 5; j++) {//显示所有按键 105 $("#" + ButtonArray[j]).show(); 106 } 107 108 if (PageCount < 5) {//根据总页数隐藏不需要显示的页码按钮 109 for (var i = PageCount; i < 5; i++) { 110 $("#" + ButtonArray[i]).hide(); 111 } 112 } 113 var ShowPageNumber = 0; 114 for (var a = 0; a < 5; a++) {//控制按键样式 115 ShowPageNumber = $("#" + ButtonArray[a]).text(); 116 if (ShowPageNumber == RealPageNumber) { 117 $("#" + ButtonArray[a]).addClass("checked").siblings().removeClass("checked"); 118 break; 119 } 120 } 121 } 122 function PageMoreButtonControl(RealPageNumber, PageCount) { 123 if (PageCount <= 5) { 124 $("#promore").hide(); 125 $("#nextmore").hide(); 126 } else { 127 if (RealPageNumber > 3 && RealPageNumber < (PageCount - 2)) { 128 $("#promore").show(); 129 $("#nextmore").show(); 130 } else if (RealPageNumber > 3) { 131 $("#promore").show(); 132 $("#nextmore").hide(); 133 } else if (RealPageNumber < (PageCount - 2)) { 134 $("#promore").hide(); 135 $("#nextmore").show(); 136 } 137 } 138 } 139 function ProPage() {//前一页 140 var oldpage = $("#CurrentPage").val(); 141 var currentpage = parseInt(oldpage) - 1 142 ChangePage(currentpage); 143 } 144 function GetPage(th) {//获取指定页 145 var pageindex = $(th).text(); 146 var currentpage = pageindex - 1; 147 ChangePage(currentpage); 148 } 149 function NextPage() {//获取下一页 150 var oldpage = $("#CurrentPage").val(); 151 var currentpage = parseInt(oldpage) + 1 152 ChangePage(currentpage); 153 } 154 function FirstPage() {//跳转到第一页 155 ChangePage(0); 156 } 157 function LastPage() {//跳转到最后一页 158 var pagecount = $("#pagecount").val(); 159 var QuaryPage = parseInt(pagecount) - 1; 160 ChangePage(QuaryPage); 161 } 162 function ChangePage(currentpage) {//判断请求页码是否在合理范围内 163 var pagecount = $("#pagecount").val(); 164 var pagesize = $("#pagesize").val(); 165 if (currentpage >= 0 && currentpage < pagecount) { 166 $("#CurrentPage").val(currentpage); 167 var requesttye = $("#requesttype").val(); 168 var par = '{"pageIndex":"' + currentpage + '","pageSize":"' + pagesize + '","classid":"' + $("#classid").val() + '","requesttye":"' + requesttye + '"}'; 169 getdata(par); 170 } 171 }
1 <input type="hidden" id="CurrentPage" value="0"/> 2 <input type="hidden" id="pagesize" value="10" /> 3 <input type="hidden" id="pagecount" /> 4 5 <div id="pagediv"> 6 <ul> 7 <li id="firstpage" onclick="FirstPage();"><img src="/images/firstpage.jpg" /></li> 8 <li id="propage" onclick="ProPage();"> 9 <img src="/images/page_front.png" /></li> 10 <li class="page-more" id="promore"> 11 <img src="/images/page_more.jpg" /></li> 12 <li class="checked" id="pageone" onclick="GetPage(this);"></li> 13 <li id="pagetwo" onclick="GetPage(this);"></li> 14 <li id="pagethree" onclick="GetPage(this);"></li> 15 <li id="pagefour" onclick="GetPage(this);"></li> 16 <li id="pagefive" onclick="GetPage(this);"></li> 17 <li class="page-more" id="nextmore"> 18 <img src="/images/page_more.jpg" /></li> 19 <li id="nextpage" onclick="NextPage();"> 20 <img src="/images/page_last.png" /></li> 21 <li id="lastpage" onclick="LastPage();"><img src="/images/lastpage.png" /></li> 22 </ul> 23 </div>
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; int type = int.Parse(context.Request["type"]); string result = string.Empty; int index = int.Parse(context.Request["index"]); int size = int.Parse(context.Request["size"]); result = GetEnterpriseHonor(12, index, size); } private string GetEnterpriseHonor(int type, int index, int size) { DataTable dt = qd.SigleTabelPaging("iwms_news", "title,content", "classid=" + type, index, size, "articleid"); if (dt.Rows.Count > 0) { Regex reg = new Regex(@"upload.*?\.[a-z]{3,}");//图片地址正则 StringBuilder honorsb = new StringBuilder(); foreach (DataRow dr in dt.Rows) { string tem = dr["title"].ToString(); MatchCollection mcs = reg.Matches(dr["content"].ToString());//抓取图片地址 if (mcs.Count > 1) { if(tem.Length>10){ honorsb.Append("<li onclick=\"HonorClick('/" + mcs[1].ToString() + "')\"><a href='#'><img src='/" + mcs[0].ToString() + "'></a><a href='#'>" + tem.Substring(0, 10) + "......</a></li>"); }else{ honorsb.Append("<li onclick=\"HonorClick('/" + mcs[1].ToString() + "')\"><a href='#'><img src='/" + mcs[0].ToString() + "'></a><a href='#'>" + tem+ "</a></li>"); } } else { if (tem.Length > 10) { honorsb.Append("<li onclick=\"HonorClick('/" + mcs[0].ToString() + "')\"><a href='#'><img src='/" + mcs[0].ToString() + "'></a><a href='#'>" + tem.Substring(0, 10) + "......</a></li>"); } else { honorsb.Append("<li onclick=\"HonorClick('/" + mcs[0].ToString() + "')\"><a href='#'><img src='/" + mcs[0].ToString() + "'></a><a href='#'>" + tem + "</a></li>"); } } //honorsb.Append("<li onclick=\"HonorClick('"图片地址"')\"><a href='#'>" + dr["content"].ToString().Replace("upload/", "/upload/") + "</a><a href='#'>" + tem.Substring(tem.LastIndexOf('_') + 1) + "</a></li>"); } honorsb.Append("@=" + dt.Rows[0]["rc"].ToString()); return honorsb.ToString(); } return ""; } private class MessageContent { /// <summary> /// 标识 /// </summary> public string id { get; set; } /// <summary> /// 分类id /// </summary> public string classid {get;set;} /// <summary> /// 页大小 /// </summary> public int pageSize { get; set; } /// <summary> /// 第几页 /// </summary> public int pageIndex { get; set; } /// <summary> /// 主要内容 /// </summary> public string content { get; set; } /// <summary> /// 总页数 /// </summary> public string pageCount { get; set; } /// <summary> /// 获取数据类型 /// </summary> public string requesttye { get; set; } } public bool IsReusable { get { return false; } }