一个分页显示程序,没页显示5个数据,大家帮帮忙!!
http://www.csdn.net/develop/Read_Article.asp?Id=15464
http://www.csdn.net/develop/Read_Article.asp?Id=6860
http://www.csdn.net/develop/Read_Article.asp?Id=16257
<%
dim i,ye
i=0
rs.PageSize=10
if dj = "" or dj <= 1then
rs.AbsolutePage =1
ye=1
elseif dj >= rs.pagecount then
rs.absolutepage=rs.PageCount
ye=rs.PageCount
else
rs.absolutepage=dj
end if
%>
<%
sql="select * from tablename"
rs.open sql,conn,1,1
if rs.eof and rs.bof then
response.write "<p align=center> 还 没 有 任 何 文 章</p>"
else
totalPut=rs.recordcount
totalPut=rs.recordcount
if currentpage<1 then
currentpage=1
end if
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
rs2.move (currentPage-1)*MaxPerPage
dim bookmark
bookmark=rs2.bookmark
showpage totalput,MaxPerPage,"xxxxxx.asp"
showContent
showpage totalput,MaxPerPage,"xxxxx.asp"
else
currentPage=1
showpage totalput,MaxPerPage,"xxxxx.asp"
showContent
showpage totalput,MaxPerPage,"xxxxx.asp"
end if
end if
rs.close
end if
set rs=nothing
conn.close
set conn=nothing
sub showContent
dim iii
iii=0
%>
<div align="center">
<table><tr><td>这里放你的数据</td></tr>
<% iii=iii+1
if iii>=MaxPerPage then exit do
rs.movenext
loop
%>
</table>
<%
end sub
function showpage(totalnumber,maxperpage,filename)
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&"?typeid="&typeid&">"
response.write "<p align=center> "
if CurrentPage<2 then
response.write "<font color=#000080>首页 上一页</font> "
else
response.write "<a href="&filename&"?page=1&typeid="&typeid&">首页</a> "
response.write "<a href="&filename&"?page="&CurrentPage-1&"&typeid="&typeid&">上一页</a> "
end if
if n-currentpage<1 then
response.write "<font color=#000080>下一页 尾页</font>"
else
response.write "<a href="&filename&"?page="&(CurrentPage+1)&"&typeid="&typeid&">"
response.write "下一页</a> <a href="&filename&"?page="&n&"&typeid="&typeid&">尾页</a>"
end if
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
%>
。
进行 ADO 存取数据库时的分页显示,其实就是对 Recordset 的记录进行操作。所以我们首先必须了解 Reordset 对象的属性和方法:
BOF 属性:目前指标指到 RecordSet 的第一笔。
EOF 属性:目前指标指到 RecordSet 的最后一笔。
Move 方法:移动指标到 RecordSet 中的某一条记录。
AbsolutePage 属性:设定当前记录的位置是位于哪一页 AbsolutePosition 属性:目前指标在 RecordSet 中的位置。
PageCount 属性:显示 Recordset 对象包括多少“页”的数据。
PageSize 属性:显示 Recordset 对象每一页显示的记录数。
RecordCount 属性:显示 Recordset 对象记录的总数。
下面让我们来详细认识一下这些重要的属性和方法
一、 BOF 与 EOF 属性
通常我们在 ASP 程序中编写代码来检验 BOF 与 EOF 属性,从而得知目前指标所指向的 RecordSet 的位置,使用 BOF 与 EOF 属性,可以得知一个 Recordset 对象是否包含有记录或者得知移动记录行是否已经超出该 Recordset 对象的范围。
如: < % if not rs.eof then ... %>
< % if not (rs.bof and rs.eof) %>
若当前记录的位置是在一个 Recordset 对象第一行记录之前时, BOF 属性返回 true,反之则返回 false。
若当前记录的位置是在一个 Recordset 对象最后一行记录之后时, EOF 属性返回 true,反之则返回 false。
BOF 与 EOF 都为 False:表示指标位于 RecordSet 的当中。
BOF 为 True:目前指标指到 RecordSet 的第一笔记录。 EOF 为 True:目前指标指到 RecordSet 的最后一笔记录。
BOF 与 EOF 都为 True:在 RecordSet 里没有任何记录。
二、 Move 方法
您可以用 Move 方法移动指标到 RecordSet 中的某一笔记录,语法如下:
rs.Move NumRecords,Start
这里的“rs”为一个对象变量,表示一个想要移动当当前记录位置的 Recordset 对象;“NumRecords”是一个正负数运算式,设定当前记录位置的移动数目;“start”是一个可选的项目,用来指定记录起始的标签。
所有的 Recordset 对象都支持 Move 方法,如果 NumRecords 参数大于零,当前记录位置向末尾的方向移动;如果其小于零,则当前记录位置向开头的方向移动;如果一个空的 Recordset 对象调用 Move 方法,将会产生一个错误。
MoveFirst 方法:将当前记录位置移至第一笔记录。
MoveLast 方法:将当前记录位置移至最后一笔记录。
MoveNext 方法:将当前记录位置移至下一笔记录。 MovePrevious 方法:将当前记录位置移至上一笔记录。
Move [n] 方法:移动指标到第 n 笔记录, n 由 0 算起。
三、 AbsolutePage 属性
AbsolutePage 属性设定当前记录的位置是位于哪一页的页数编号;使用 PageSize 属性将 Recordset 对象分割为逻辑上的页数,每一页的记录数为 PageSize( 除了最后一页可能会有少于 PageSize 的记录数 )。这里必须注意并不是所有的数据提供者都支持此项属性,因此使用时要小心。
与 AbsolutePosition 属性相同, AbsolutePage 属性是以 1 为起始的,若当前记录为 Recordset 的第一行记录, AbsolutePage 为 1。可以设定 AbsolutePage 属性,以移动到一个指定页的第一行记录位置。
四、 AbsolutePosition 属性
若您需要确定目前指标在 RecordSet 中的位置,您可以用 AbsolutePosition 属性。
AbsolutePosition 属性的数值为目前指标相对於第一笔的位置,由 1 算起,即第一笔的 AbsolutePosition 为 1。
注意 , 在存取 RecordSet 时,无法保证 RecordSet 每次都以同样的顺序出现。
若要启用 AbsolutePosition,必须先设定为使用用户端 cursor( 指针 ), asp 码如下:
rs2.CursorLocation = 3
五、 PageCount 属性
使用 PageCount 属性,决定 Recordset 对象包括多少“页”的数据。这里的“页”是数据记录的集合,大小等于 PageSize 属性的设定,即使最后一页的记录数比 PageSize 的值少,最后一页也算是 PageCount 的一页。必须注意也并不是所有的数据提供者都支持此项属性。
六、 PageSize 属性
PageSize 属性是决定 ADO 存取数据库时如何分页显示的关键,使用它就可以决定多少记录组成一个逻辑上的“一页”。设定并建立一个页的大小,从而允许使用 AbsolutePage 属性移到其它逻辑页的第一条记录。 PageSize 属性能随时被设定。
七、 RecordCount 属性
这也是一个非常常用和重要的属性,我们常用 RecordCount 属性来找出一个 Recordset 对象包括多少条记录。如: < % totle=RS.RecordCount %>
在了解了 Recordset 对象的以上属性和方法后,我们来考虑一下,如何运用它们来达到我们分页显示的目的。首先,我们可以为 PageSize 属性设置一个值,从而指定从记录组中取出的构成一个页的行数;然后通过 RecordCount 属性来确定记录的总数;再用记录总数除以 PageSize 就可得到所显示的页面总数;最后通过 AbsolutePage 属性就能完成对指定页的访问。好象很并不复杂呀,下面让我们来看看程序该如何实现呢?
我们建立这样一个简单的 BBS 应用程序,它的数据库中分别有以下五个字段:“ID”,每个帖子的自动编号;“subject”,每个帖子的主题;“name”,加帖用户的姓名;“email”,用户的电子邮件地址;“postdate”,加帖的时间。数据库的 DSN 为“bbs”。我们将显示帖子分页的所有步骤放在一个名为“ShowList()”的过程中,方便调用。程序如下:
----BBS 显示帖子分页----
< % Sub ShowList() %>
< %
PgSz=20 设定开关,指定每一页所显示的帖子数目,默认为20帖一页
Set Conn = Server.CreateObject("ADODB.Connection")
Set RS = Server.CreateObject("ADODB.RecordSet")
sql = "SELECT * FROM message order by ID DESC"
查询所有帖子,并按帖子的ID倒序排列
Conn.Open "bbs"
RS.open sql,Conn,1,1
If RS.RecordCount=0 then
response.write "< P>< center>对不起,数据库中没有相关信息!< /center>< /P>"
else
RS.PageSize = Cint(PgSz) 设定PageSize属性的值
Total=INT(RS.recordcount / PgSz * -1)*-1 计算可显示页面的总数
PageNo=Request("pageno")
if PageNo="" Then
PageNo = 1
else
PageNo=PageNo+1
PageNo=PageNo-1
end if
ScrollAction = Request("ScrollAction")
if ScrollAction = " 上一页 " Then
PageNo=PageNo-1
end if
if ScrollAction = " 下一页 " Then
PageNo=PageNo+1
end if
if PageNo < 1 Then
PageNo = 1
end if
n=1
RS.AbsolutePage = PageNo
Response.Write "< CENTER>"
position=RS.PageSize*PageNo
pagebegin=position-RS.PageSize+1
if position < RS.RecordCount then
pagend=position
else
pagend= RS.RecordCount
end if
Response.Write "< P>< font color=Navy>< B>数据库查询结果:< /B>"
Response.Write "(共有"&RS.RecordCount &"条符合条件的信息,显示"&pagebegin&"-"&pagend&")< /font>< /p>"
Response.Write "< TABLE WIDTH=600 BORDER=1 CELLPADDING=4 CELLSPACING=0 BGCOLOR=#FFFFFF>"
Response.Write "< TR BGCOLOR=#5FB5E2>< FONT SIZE=2>< TD>< B>主题< /B>< /TD>< TD>< B>用户< /B>< /TD>< TD>< B>Email< /B>< /TD>< TD>< B>发布日期< /B>< /TD>< /FONT>< TR BGCOLOR=#FFFFFF>"
Do while not (RS is nothing)
RowCount = RS.PageSize
Do While Not RS.EOF and rowcount > 0
If n=1 then
Response.Write "< TR BGCOLOR=#FFFFFF>"
ELSE
Response.Write "< TR BGCOLOR=#EEEEEE>"
End If
n=1-n %>
< TD>< span style="font-size:9pt">< A href=view.asp?key=< % =RS("ID")%>>< % =RS("subject")%>< /A>< /span>< /td>
< TD>< span style="font-size:9pt">< % =RS("name")%>< /A>< /span>< /td>
< TD>< span style="font-size:9pt">< a href="mailto:< % =RS("email")%>">< % =RS("email")%>< /a>< /span> < /TD>
< TD>< span style="font-size:9pt">< % =RS("postdate")%>< /span> < /td>
< /TR>
< %
RowCount = RowCount - 1
RS.MoveNext
Loop
set RS = RS.NextRecordSet
Loop
Conn.Close
set rs = nothing
set Conn = nothing
%>
< /TABLE>
< FORM METHOD=GET ACTION="list.asp">
< INPUT TYPE="HIDDEN" NAME="pageno" VALUE="< % =PageNo %>">
< %
if PageNo > 1 Then
response.write "< INPUT TYPE=SUBMIT NAME=ScrollAction VALUE= 上一页 >"
end if
if RowCount = 0 and PageNo < >Total then
response.write "< INPUT TYPE=SUBMIT NAME=ScrollAction VALUE= 下一页 >"
end if
response.write "< /FORM>"
End if
%>
< % End Sub %>
相信大家都应该能完全读懂上面的程序,因此作者就不在此详细解释了。值得注意的是在这段程序中运用了一个小技巧 < INPUT TYPE="HIDDEN" NAME="pageno" VALUE="< % =PageNo %>">,这是用来在每次调用该 ASP 文件时传递数据的“暗道”,由于我们需要在每次调用程序时传递代表当前页码的参数,可能大家会想到使用 session,但是从节省系统资源和通用性来讲,用这样一个隐藏的 form 来传递数据将会达到更好的效果。