Asp.Net之JS生成分页条的方法
程序员文章站
2022-04-10 11:19:02
...
Default.aspx.cs中的代码
protected int pageIndex = 1; protected int pageSize = 10; protected int pageCount = 100; protected string name = string.Empty; protected void Page_Load(object sender, EventArgs e) { int.TryParse(Request.QueryString["pageIndex"],out pageIndex); name=Request.QueryString["name"]; }
Default.aspx.cs中的代码
Body中的内容
名称:<input type="text" id="txtName" value="<%=name %>" /> <div id="PageBar">
JS中的内容
<script src="jquery-1.4.1.js" type="text/javascript"></script> <script type="text/javascript"> var pagecount=<%=pageCount %>; var pageindex=<%=pageIndex %>; var pageSize=<%=pageSize %>; $(function(){ createPageBar(pageindex,pagecount); }) //生成分页条 function createPageBar(pageindex,pageCount){ //拿到存放分页条的div并清空 var pageBarObj=$('#PageBar'); pageBarObj.html(''); //判断给定页码 if(pageindex<1){ pageindex=1; } if(pageindex>pageCount){ pageindex=pagecount; } //首页与上一页 $('<a href="javaScript:void(0)">首页</a> ').appendTo(pageBarObj).click(function(){ goPage(1); }); if(pageindex>1){ $('<a href="javaScript:void(0)">上一页</a> ').appendTo(pageBarObj).click(function(){ goPage(pageindex-1); }); } //数字分页 var start=pageindex-4; if(start<1){ start=1; } var end=start+9; if(end>pagecount){ end=pagecount; } for(var i=start;i<=end;i++){ $(' <a href="javascript:void(0)">['+i+']</a> ').appendTo(pageBarObj).click(function(){ goPage(i); }); } //下一页与尾页 if(pageindex<pagecount){ $('<a href="javaScript:void(0)">下一页</a> ').appendTo(pageBarObj).click(function(){ goPage(pageindex+1); }); } $('<a href="javaScript:void(0)">尾页</a>').appendTo(pageBarObj).click(function(){ goPage(pagecount); }); } //去跳转 function goPage(pageindex){ var name=$('#txtName').val(); window.location="/Default.aspx?pageindex="+pageindex+"&name="+name; } </script>