JS+ASP实现无刷新新闻列表之分页
作者:dnawo 来源:mzwu.com 时间:2007-08-22 12:57:00
上次用Javascript+ASP实现了无刷新的新闻列表,最后还有一个小问题没有解决:下边的分页数列"首页、上10页、下10页、尾页"间怎么切换?你应该不会想用传统的方法用ASP进行查询然后显示数列吧?那我们之前对无刷新所做的努力可就全白费了!
你首先应该想到的是再用Javascript+ASP实现无刷新显示数列,但这次我们也不用这个,我们有更简单的方法。
使用Javascript+ASP来显示分页数列的话那切换一次就得重新查询一次数据库,完全没有必要!我们只需在打开新闻列表页时查询一次数据库,将总页数保存起来(比如保存在一个隐藏的表单中),以后列表的切换工作就交给Javascript了,还是Javascript+ASP,不过本质不一样了哦,先前是同步,这次是异步了。
具体过程:
1.在页面中新建两个隐藏的表单域,其中一个用于存放当前列表的最后一项,另一个用于存放总页数:
<input name="endid" id="endid" type="hidden" value="0">
<input name="countnews" 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中.
至此,整个新闻列表显示页就完整了。最后说一句:凡事,首先从简单的出发...
相关阅读:
标签:无刷新,ASP,JS,分页
0
投稿
猜你喜欢
互联网产品设计零碎记
2010-09-25 12:49:00
亲自教你使用 ChatGPT 编写 SQL JOIN 查询示例
2024-01-14 18:54:01
logging level级别介绍
2023-06-07 00:39:16
基于Python Dash库制作酷炫的可视化大屏
2022-09-13 14:20:32
MYSQL初学者使用指南[适用自己安装mysql者]
2007-08-06 14:53:00
VSCode下配置python调试运行环境的方法
2023-03-27 10:15:40
JS操作input标签属性checkbox全选的实现代码
2024-04-28 09:50:56
python爬虫爬取淘宝商品比价(附淘宝反爬虫机制解决小办法)
2021-11-14 06:16:40
Javascript Closures (2)
2009-03-18 12:22:00
详解python 3.6 安装json 模块(simplejson)
2023-08-04 10:55:03
HTML5 离线存储之Web SQL
2011-06-19 14:13:19
Python 动态变量名定义与调用方法
2023-07-29 22:36:05
MySQL IFNULL判空问题解决方案
2024-01-21 13:23:10
浅谈python为什么不需要三目运算符和switch
2022-04-02 16:22:39
详解如何使用beego orm在postgres中存储图片
2024-04-25 15:14:46
教你如何编写Vue.js的单元测试的方法
2024-04-26 17:38:09
Django中自定义模型管理器(Manager)及方法
2022-12-01 17:53:05
sql server 锁表语句分享
2012-02-12 15:49:20
基于Pytorch实现的声音分类实例代码
2021-01-04 21:42:28
python读取与处理netcdf数据方式
2021-11-09 02:45:01