asp.net分页代码(教你怎么实现)
直接上代码:
css部分代码
.pageCss {
float: right;
width: auto;
height: 30px;
line-height: 30px;
margin-right: 10px;
}
.pageCss a {
float: left;
color: #000;
margin-left: 10px;
text-decoration: none;
}
.pageCss span {
float: left;
margin-left: 10px;
overflow: hidden;
}
.pageCss span font {
font-size: 13px;
font-weight: bold;
}
.txtManagePage {
float: left;
margin: 7px 0 0 10px;
width: 38px;
height: 14px;
border: 1px solid #fff;
}
.pageGo {
display: block;
margin-top: 7px;
width: 38px;
height: 16px;
text-align: center;
line-height: 16px;
border: 1px solid #fff;
cursor: pointer;
}
text.aspx.cs 代码
//定义的一些变量
//int pageCount = 11;//每11行数据分为一页
//public int supNum = 0;//总页数
//int page = 1;//当前页数
//public string paging = string.Empty;//这个主要用于前台绑定数据
//第一步首先创建一个公用的方法
public void GetPage(DataTable dt)
{
int rec = 0;
if (dt != null)
{
rec = int.Parse(dt.Rows[0][0].ToString());
if (rec < pageCount)
{
supNum = 1;
}
if (rec >= pageCount && rec % pageCount == 0)
{
supNum = rec / pageCount;
}
else
{
supNum = rec / pageCount + 1;
}
}
string homePage = "<a href='?mp=1'>首页</a>";//首页
string lastPage = "<a href='?mp=" + supNum + "'>末页</a>";//末页
string back = string.Empty;
//上一页
if (page == 1)
{
back = "<span>上一页</span>";
}
else
{
back = "<a href='?mp=" + (page - 1) + "'>上一页</a>";
}
string next = string.Empty;
//下一页
if (page == supNum)
{
next = "<span>下一页</span>";
}
else
{
next = "<a href='?mp=" + (page + 1) + "'>下一页</a>";
}
paging = homePage + back + next + lastPage +
//修改
"<span>跳转到</span><input id='txtManagePage' type='text' class='txtManagePage' value='" + page + "' onkeyup=\"this.value=this.value.replace(/\\D/g,'')\" /><span>页</span>" +
"<span class='pageGo'>GO</span>" +
"<span>当前第<font>" + page + "</font>页</span>" +
"<span>共<font>" + supNum + "</font>页</span>" +
"<span>共<font>" + rec + "</font>条数据</span>" +
"<span>本页显示<font>" + pageCount + "</font>条数据</span>";
}
这里的mp的意思是:获取跳转页数。
在 protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["mp"] != null)
{
page = int.Parse(Request.QueryString["mp"]);
}
注意要类型转化。
}
text.aspx 部分
<div class="pageCss">
<%=paging %>
</div>
前台调用方法。
js部分:
//跳转分页
$(".pageGo").click(function () {
var txtPage = $("#txtManagePage").val();
//获取当前输入的页数
var supNum = '<%=supNum%>';
//获取总页数现在我们来判断一下
if (txtPage > supNum) {
txtPage = supNum;
}
if (txtPage < 1) {
txtPage = 1;
}
window.location.href = "alimama_browse.aspx?mp=" + txtPage;
});
其实最核心的是查询代码:(如下:在查询中写入即可)
select top " + pageCount + " * from tb_text where alimama_Id not in(select top " + (page - 1) * pageCount + " alimama_Id from tb_text order by alimama_Id desc) order by alimama_Id desc
这条语句有个好处:
1.前面用*,子查询中用id.加快查询速度
分页调用如下:
GetPage(sqlhelper.publicFun("select count(alimama_Id) from tb_text"));