使用Javascript+ASP来显示分页数列的话那切换一次就得重新查询一次数据库,完全没有必要!我们只需在打开新闻列表页时查询一次数据库,将总页数保存起来(比如保存在一个隐藏的表单中),以后列表的切换工作就交给Javascript了,还是Javascript+ASP,不过本质不一样了哦,先前是同步,这次是异步了。
实现方法:
1.在页面中新建两个隐藏的表单域,其中一个用于存放当前列表的最后一项,另一个用于存放总页数:
<input name="endid" id="endid" type="hidden" value="0">
<input name="countpage" id="countpage" type="hidden" value="0">
2.新建div标签用于存放分页数列:
<div id="pageNews"></div>
3.加入Javascript脚本:
//首页
function first(){
var table,endid,countpage,i;
endid = parseInt(document.getElementById("endid").value);
countpage = parseInt(document.getElementById("countpage").value);
table = "<table height='20' border='0' align='center' cellpadding='0' cellspacing='0'><tr>";
table += "<td width='20' align='center' valign='middle'><a href='javascript:first();'><img src='images/First.gif' width='9' height='8' border='0' title='首页'></a></td>";
if(countpage<=10){
for(i=1;i<=countpage;i++){
table += "<td width='20' align='center' valign='middle'><a href='javascript:showpage(" + i + ");'>" + i + "</a></td>";
}
document.getElementById("endid").value = countpage;
}else{
for(i=1;i<=10;i++){
table += "<td width='20' align='center' valign='middle'><a href='javascript:showpage(" + i + ");'>" + i + "</a></td>";
}
document.getElementById("endid").value = "10";
table += "<td width='20' align='center' valign='middle'><a href='javascript:next();'><img src='images/Next.gif' width='8' height='8' border='0'title='下10页'></a></td>"
}
table += "<td width='20' align='center' valign='middle'><a href='javascript:last();'><img src='images/Last.gif' width='9' height='8' border='0'title='尾页'></a></td>";
table += "</tr></table>";
document.getElementById("pageNews").innerHTML = table;
}
//上10页
function previous(){
var table,endid,countpage,i;
endid = parseInt(document.getElementById("endid").value);
countpage = parseInt(document.getElementById("countpage").value);
table = "<table height='20' border='0' align='center' cellpadding='0' cellspacing='0'><tr>";
table += "<td width='20' align='center' valign='middle'><a href='javascript:first();'><img src='images/First.gif' width='9' height='8' border='0' title='首页'></a></td>";
if(endid-20<1){
for(i=1;i<=10;i++){
table += "<td width='20' align='center' valign='middle'><a href='javascript:showpage(" + i + ");'>" + i + "</a></td>";
}
document.getElementById("endid").value = "10";
table += "<td width='20' align='center' valign='middle'><a href='javascript:next();'><img src='images/Next.gif' width='8' height='8' border='0'title='下10页'></a></td>"
}else{
table += "<td width='20' align='center' valign='middle'><a href='javascript:previous();'><img src='images/Previous.gif' width='8' height='8' border='0'title='上10页'></a></td>";
for(i = endid-19;i<= endid-10;i++){
table += "<td width='20' align='center' valign='middle'><a href='javascript:showpage(" + i + ");'>" + i + "</a></td>";
}
document.getElementById("endid").value = endid-10;
table += "<td width='20' align='center' valign='middle'><a href='javascript:next();'><img src='images/Next.gif' width='8' height='8' border='0'title='下10页'></a></td>"
}
table += "<td width='20' align='center' valign='middle'><a href='javascript:last();'><img src='images/Last.gif' width='9' height='8' border='0'title='尾页'></a></td>";
table += "</tr></table>";
document.getElementById("pageNews").innerHTML = table;
}
//下10页
function next(){
var endid,table,countpage,i;
endid = parseInt(document.getElementById("endid").value);
countpage = parseInt(document.getElementById("countpage").value);
table = "<table height='20' border='0' align='center' cellpadding='0' cellspacing='0'><tr>";
table += "<td width='20' align='center' valign='middle'><a href='javascript:first();'><img src='images/First.gif' width='9' height='8' border='0' title='首页'></a></td>";
if(endid + 10>countpage){
table += "<td width='20' align='center' valign='middle'><a href='javascript:previous();'><img src='images/Previous.gif' width='8' height='8' border='0'title='上10页'></a></td>";
for(i=endid + 1;i<=countpage;i++){
table += "<td width='20' align='center' valign='middle'><a href='javascript:showpage(" + i + ");'>" + i + "</a></td>";
}
document.getElementById("endid").value = countpage;
}else{
table += "<td width='20' align='center' valign='middle'><a href='javascript:previous();'><img src='images/Previous.gif' width='8' height='8' border='0'title='上10页'></a></td>";
for(i=endid + 1;i<=endid + 10;i++){
table += "<td width='20' align='center' valign='middle'><a href='javascript:showpage(" + i + ");'>" + i + "</a></td>";
}
document.getElementById("endid").value = endid + 10;
table += "<td width='20' align='center' valign='middle'><a href='javascript:next();'><img src='images/Next.gif' width='8' height='8' border='0'title='下10页'></a></td>"
}
table += "<td width='20' align='center' valign='middle'><a href='javascript:last();'><img src='images/Last.gif' width='9' height='8' border='0'title='尾页'></a></td>";
table += "</tr></table>";
document.getElementById("pageNews").innerHTML = table;
}
//尾页
function last(){
var table,endid,countpage,i;
endid = parseInt(document.getElementById("endid").value);
countpage = parseInt(document.getElementById("countpage").value);
table = "<table height='20' border='0' align='center' cellpadding='0' cellspacing='0'><tr>";
table += "<td width='20' align='center' valign='middle'><a href='javascript:first();'><img src='images/First.gif' width='9' height='8' border='0' title='首页'></a></td>";
if(countpage<=10){
for(i=1;i<=countpage;i++){
table += "<td width='20' align='center' valign='middle'><a href='javascript:showpage(" + i + ");'>" + i + "</a></td>";
}
document.getElementById("endid").value = countpage;
}else{
table += "<td width='20' align='center' valign='middle'><a href='javascript:previous();'><img src='images/Previous.gif' width='8' height='8' border='0'title='上10页'></a></td>";
for(i=countpage - 9;i<=countpage;i++){
table += "<td width='20' align='center' valign='middle'><a href='javascript:showpage(" + i + ");'>" + i + "</a></td>";
}
document.getElementById("endid").value = countpage;
}
table += "<td width='20' align='center' valign='middle'><a href='javascript:last();'><img src='images/Last.gif' width='9' height='8' border='0'title='尾页'></a></td>";
table += "</tr></table>";
document.getElementById("pageNews").innerHTML = table;
}
4.将<script language="JavaScript" type="text/javascript">first();</script>放在<div id="pageNews"></div>后,用于初始化分页列表并将结果显示于div中。
5.在news.asp中加入程序,在页面打开时查询数据库,并将总页数保存于countnews中。
至此,整个新闻列表显示页就完整了。最后说一句:凡事,首先从简单的出发...
评论(19)
务必先看完"Javascript+ASP打造无刷新新闻列表"再看本文
请问可否给出详细的该页代码~?~
或者提供原文件下载~多谢!~
盼望给出news.asp页的代码~多谢!~
可是~如何才能实现显示“当前第i页”的功能呢?~
document.getElementById("nowpage").innerText = "当前第" + n + "页";
在页面恰当位置加标签:<div id="nowpage"></div>
^_^
会不显示当前在第几页~而当点击了其他页数后~就没有问题了~这个该如何解决呢?~
搞来搞去还是没搞明白...
源码诉不能提供,不过你可以说说看你的错误信息
Sql = "select * from news"
Set rs = Server.CreateObject("ADODB.RecordSet")
Rs.open sql,conn,1,1
Rs.pagesize = 3
countnews=rs.pagecount
%>
<input name="countnews" id="countpage" type="hidden" value="<%=countnews%>">
<script language="javascript">JS代码</script>
我的代码是这样写的 那里不对了
不行加我Q:270250392