come on babay

come on babay,Love me ,love yourself.

2007/05/26

 

ASP做一个分页程序


你是否为了你的站点文章过多而反复做链接上一页、下一页这些烦琐的工作发愁呢?现在有了ASP,只要你的主页支持ASP,你就可以很简单的实现对文章的管理了,排序、链接、显示这些工作都让ASP去完成吧!这么轻松的主页维护,你是不是也想试一试呢?
  
  请看以下实现ASP分页程序的代码:

  <language="vbscript"
  
  dim conn
  
  dim connstr
  
  dim totalPut
  
  dim CurrentPage
  
  dim TotalPages
  
  dim i,j
  
  dim sql
  
  dim rs
  
  on error resume next
  
  '打开数据库
  
  connstr="DBQ="+server.mappath("book.mdb")+";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
  
  set conn= server.createobject("ADODB.CONNECTION")
  
  conn.open connstr
  
  set rs=server.createobject("adodb.recordset")
  
  '定义每页文章显示数
  
  const MaxPerPage=18
  
  if not isempty(request("page")) then
  
  currentPage=cint(request("page"))
  
  else
  
  currentPage=1
  
  end if
  
  sql="select * from learning order by articleid desc"
  
  Set rs= Server.CreateObject("ADODB.Recordset")
  
  rs.open sql,conn,1,1
  
  if rs.eof and rs.bof then
  
  response.write "<p align='center'> 还 没 有 任 何 文 章</p>"
  
  else
  
  '数据库中文章数totalput
  
  totalPut=rs.recordcount
  
  if currentpage<1 then
  
  currentpage=1
  
  end if
  
  '统计总页数currentpage
  
  if (currentpage-1)*MaxPerPage>totalput then
  
  if (totalPut mod MaxPerPage)=0 then
  
  currentpage= totalPut \ MaxPerPage
  
  else
  
  currentpage= totalPut \ MaxPerPage + 1
  
  end if
  
  end if
  
  if currentPage=1 then
  
  showpage totalput,MaxPerPage,"index.asp"
  
  showContent
  
  showpage totalput,MaxPerPage,"index.asp"
  
  else
  
  if (currentPage-1)*MaxPerPage<totalPut then
  
  rs.move (currentPage-1)*MaxPerPage
  
  dim bookmark
  
  bookmark=rs.bookmark
  
  showpage totalput,MaxPerPage,"index.asp"
  
  showContent
  
  showpage totalput,MaxPerPage," index.asp"
  
  else
  
  currentPage=1
  
  showpage totalput,MaxPerPage,"index.asp"
  
  showContent
  
  showpage totalput,MaxPerPage,"index.asp"
  
  end if
  
  end if
  
  rs.close
  
  end if
  
  set rs=nothing
  
  conn.close
  
  set conn=nothing
  
  sub showContent
  
  dim i
  
  i=0
  
  do while not rs.eof
  
  >
  
  '选择显示数据库内容
  
  <a href="openarticle.asp?id=<=rs("articleid")>"><=rs("title")></a>[点击:<=rs("hits")>]<br>
  
  <
  
  '当显示记录大于maxperpage时结束这页
  
  i=i+1
  
  if i>=MaxPerPage then exit do
  
  rs.movenext
  
  loop
  
  end sub
  
  function showpage(totalnumber,maxperpage,filename)
  
  '求出当每页18篇文章时总共的页数
  
  dim n
  
  if totalnumber mod maxperpage=0 then
  
  n= totalnumber \ maxperpage
  
  else
  
  n= totalnumber \ maxperpage+1
  
  end if
  
  response.write "<form method=Post action="&filename&">"
  
  response.write "<p align='center'><font color='#000080'>>>分页</font> "
  
  '显示页数链接的条件
  
  if CurrentPage<2 then
  
  response.write "<font color='#000080'>首页 上一页</font> "
  
  else
  
  response.write "<a href=" http://www.webjx.com/htmldata/2006-03-06/&filename&"?page=1&>首页</a>"
  
  response.write "<a href=" http://www.webjx.com/htmldata/2006-03-06/&filename&"?page="&CurrentPage-1&">上一页</a>"
  
  end if
  
  if n-currentpage<1 then
  
  response.write "<font color='#000080'>下一页 尾页</font>"
  
  else
  
  response.write "<a href="&filename&"?page="&(CurrentPage+1)
  
  response.write ">下一页</a> <a href="http://www.webjx.com/htmldata/2006-03-06/&filename&"?page="&n &">尾页</a>"
  
  end ifc
  
  response.write "<font color='#000080'> 页次:</font><strong><font color=red>"&CurrentPage&"</font><font color='#000080'>/"&n&"</strong>页</font>"
  
  response.write "<font color='#000080'> 共<b>"&totalnumber&"</b>篇文章 <b>"&maxperpage&"</b>篇文章/页</font>"
  
  response.write " <font color='#000080'>转到:</font><input type='text' name='page' size=4 maxlength=10 class=smallInput value="¤tpage&">"
  
  response.write "<input class=buttonface type='submit' value=' Goto ' name='cndok'></span></p></form>"
  
  end function
  
  >

  以上代码很简单的就实现了ASP网页的多种分页功能,不论是对浏览者还是管理者都能很方便的浏览和管理,
--
我的网络营销boogger
http://china-news-it.blogspot.com






<< 主页

存档

2007/4/8 - 2007/4/15   2007/4/15 - 2007/4/22   2007/4/22 - 2007/4/29   2007/4/29 - 2007/5/6   2007/5/6 - 2007/5/13   2007/5/13 - 2007/5/20   2007/5/20 - 2007/5/27   2007/5/27 - 2007/6/3   2007/6/3 - 2007/6/10   2007/6/10 - 2007/6/17   2007/6/17 - 2007/6/24   2007/6/24 - 2007/7/1   2007/7/8 - 2007/7/15   2007/12/2 - 2007/12/9   2007/12/9 - 2007/12/16   2007/12/16 - 2007/12/23   2009/10/18 - 2009/10/25   2010/1/10 - 2010/1/17  

This page is powered by Blogger. Isn't yours?

订阅 博文 [Atom]