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中.
 
至此,整个新闻列表显示页就完整了。最后说一句:凡事,首先从简单的出发...

相关阅读:

JS+ASP实现无刷新新闻列表方法

标签:无刷新,ASP,JS,分页
0
投稿

猜你喜欢

  • ASP关于SQL插入数据后获得当前ID

    2010-01-24 19:55:00
  • 网友分享:Oracle数据库开发技术经验浅谈

    2009-04-22 13:11:00
  • Oracle对两个数据表交集的查询

    2010-07-26 12:51:00
  • IE6中隐形的PNG8图片

    2009-11-27 18:38:00
  • Oracle PL/SQL入门慨述

    2010-07-18 12:57:00
  • 探讨关于404错误页面设置的问题

    2011-12-01 10:59:38
  • 为你的网页添加背景音乐

    2007-02-03 11:39:00
  • 影响ORACLE汉字显示的字符集问题

    2008-06-13 16:49:00
  • ASP中取得图片宽度和高度

    2009-11-08 18:39:00
  • 如何从ASP连接到Oracle Server?

    2009-11-15 19:52:00
  • 产品设计与用户体验

    2009-02-02 10:15:00
  • SQL Server 2005 五个动态管理对象

    2008-12-18 14:50:00
  • 百度百科的图片轮播代码

    2009-05-06 12:58:00
  • ASP实现类似Java中的Linked HashMap类

    2010-04-03 20:49:00
  • 电子商务搜索LIST页面用户体验设计

    2010-08-03 12:57:00
  • Oracle 10g的DBA无法登录解决方案

    2009-05-24 19:38:00
  • 如何编写一个基于WEB的文件查询系统?

    2009-11-08 18:55:00
  • MySQL表设计优化与索引 (八)

    2010-10-25 19:46:00
  • 用JS访问操作iframe框架里的dom

    2008-11-10 13:05:00
  • 利用sort()和Math.random()实现元素的随机排列

    2010-10-19 12:42:00
  • asp之家 网络编程 m.aspxhome.com