用ASP做一个分页程序
你是否为了你的站点文章过多而反复做链接上一页、下一页这些烦琐的工作发愁呢?现在有了ASP,只要你的主页支持ASP,你就可以很简单的实现对文章的管理了,排序、链接、显示这些工作都让ASP去完成吧!这么轻松的主页维护,你是不是也想试一试呢? <BR> <BR> 请看以下实现ASP分页程序的代码: <BR> <BR> <anguage="vbscript" <BR> <BR> dim conn <BR> <BR> dim connstr <BR> <BR> dim totalPut <BR> <BR> dim CurrentPage <BR> <BR> dim TotalPages <BR> <BR> dim i,j <BR> <BR> dim sql <BR> <BR> dim rs <BR> <BR> on error resume next <BR> <BR> '打开数据库 <BR> <BR> connstr="DBQ="+server.mappath("book.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};" <BR> <BR> set conn=server.createobject("ADODB.CONNECTION") <BR> <BR> conn.open connstr <BR> <BR> set rs=server.createobject("adodb.recordset") <BR> <BR> '定义每页文章显示数 <BR> <BR> const MaxPerPage=18 <BR> <BR> if not isempty(request("page")) then <BR> <BR> currentPage=cint(request("page")) <BR> <BR> else <BR> <BR> currentPage=1 <BR> <BR> end if <BR> <BR> sql="select * from learning order by articleid desc" <BR> <BR> Set rs= Server.CreateObject("ADODB.Recordset") <BR> <BR> rs.open sql,conn,1,1 <BR> <BR> if rs.eof and rs.bof then <BR> <BR> response.write "<p align='center'> 还 没 有 任 何 文 章</p>" <BR> <BR> else <BR> <BR> '数据库中文章数totalput <BR> <BR> totalPut=rs.recordcount <BR> <BR> if currentpage<1 then <BR> <BR> currentpage=1 <BR> <BR> end if <BR> <BR> '统计总页数currentpage <BR> <BR> if (currentpage-1)*MaxPerPage>totalput then <BR> <BR> if (totalPut mod MaxPerPage)=0 then <BR> <BR> currentpage= totalPut \ MaxPerPage <BR> <BR> else <BR> <BR> currentpage= totalPut \ MaxPerPage + 1 <BR> <BR> end if <BR> <BR> end if <BR> <BR> if currentPage=1 then <BR> <BR> showpage totalput,MaxPerPage,"index.asp" <BR> <BR> showContent <BR> <BR> showpage totalput,MaxPerPage,"index.asp" <BR> <BR> else <BR> <BR> if (currentPage-1)*MaxPerPage<totalPut then <BR> <BR> rs.move (currentPage-1)*MaxPerPage <BR> <BR> dim bookmark <BR> <BR> bookmark=rs.bookmark <BR> <BR> showpage totalput,MaxPerPage,"index.asp" <BR> <BR> showContent <BR> <BR> showpage totalput,MaxPerPage,"index.asp" <BR> <BR> else <BR> <BR> currentPage=1 <BR> <BR> showpage totalput,MaxPerPage,"index.asp" <BR> <BR> showContent <BR> <BR> showpage totalput,MaxPerPage,"index.asp" <BR> <BR> end if <BR> <BR> end if <BR> <BR> rs.close <BR> <BR> end if <BR> <BR> set rs=nothing <BR> <BR> conn.close <BR> <BR> set conn=nothing <BR> <BR> sub showContent <BR> <BR> dim i <BR> <BR> i=0 <BR> <BR> do while not rs.eof <BR> <BR> > <BR> <BR> '选择显示数据库内容 <BR> <BR> <a href="openarticle.asp?id=<=rs("articleid")>"><=rs("title")></a>[点击:<=rs("hits")>]<br> <BR> <BR> < <BR> <BR> '当显示记录大于maxperpage时结束这页 <BR> <BR> i=i+1 <BR> <BR> if i>=MaxPerPage then exit do <BR> <BR> rs.movenext <BR> <BR> loop <BR> <BR> end sub <BR> <BR> function showpage(totalnumber,maxperpage,filename) <BR> <BR> '求出当每页18篇文章时总共的页数 <BR> <BR> dim n <BR> <BR> if totalnumber mod maxperpage=0 then <BR> <BR> n= totalnumber \ maxperpage <BR> <BR> else <BR> <BR> n= totalnumber \ maxperpage+1 <BR> <BR> end if <BR> <BR> response.write "<form method=Post action="&filename&">" <BR> <BR> response.write "<p align='center'><font color='#000080'>>>分页</font> " <BR> <BR> '显示页数链接的条件 <BR> <BR> if CurrentPage<2 then <BR> <BR> response.write "<font color='#000080'>首页 上一页</font> " <BR> <BR> else <BR> <BR> response.write "<a href="&filename&"?page=1&>首页</a> " <BR> <BR> response.write "<a href="&filename&"?page="&CurrentPage-1&">上一页</a> " <BR> <BR> end if <BR> <BR> if n-currentpage<1 then <BR> <BR> response.write "<font color='#000080'>下一页 尾页</font>" <BR> <BR> else <BR> <BR> response.write "<a href="&filename&"?page="&(CurrentPage+1) <BR> <BR> response.write ">下一页</a> <a href="&filename&"?page="&n&">尾页</a>" <BR> <BR> end ifc <BR> <BR> response.write "<font color='#000080'> 页次:</font><strong><font color=red>"&CurrentPage&"</font><font color='#000080'>/"&n&"</strong>页</font>" <BR> <BR> response.write "<font color='#000080'> 共<b>"&totalnumber&"</b>篇文章 <b>"&maxperpage&"</b>篇文章/页</font>" <BR> <BR> response.write " <font color='#000080'>转到:</font><input type='text' name='page' size=4 maxlength=10 class=smallInput value="¤tpage&">" <BR> <BR> response.write "<input class=buttonface type='submit' value=' Goto ' name='cndok'></span></p></form>" <BR> <BR> end function <BR> <BR> > <BR> <BR> 以上代码很简单的就实现了ASP网页的多种分页功能,不论是对浏览者还是管理者都能很方便的浏览和管理,我只在相关部分作了一些解释,象这个ASP程序还必须有数据库的支持才可以,关于数据库的问题我就不详述了页:
[1]
