ASP记录集分页

时间:2022-06-21 23:48:04
 

功能:分页程序首先读取每页预置的记录条数,在此是5条,其它将在下页中显示,同时提示当前页数、总页数、总记录数,当显示的页数为第一页时,“首页”、“上一页”链接失效,当显示的页数为最后页时,“下一页”、“尾页”链接失效。
接下来,以实例的方式告诉大家怎么一步步的做出这种分页效果。
首先,数据库中字段record_info存在于info表中(实例下载中有数据库),先链接数据库并将一个记录集打开,以下代码:
<%
Set conn=Server.CreateObject("Adodb.Connection")
connstr="provider=Microsoft.JET.OLEDB.4.0;Data Source="&Server.MapPath("data.mdb")
conn.open connstr
Set rs=Server.CreateObject("Adodb.Recordset")
sql="Select * from info"
rs.open sql,conn,1,1
%>
这段代码不详解,相信初入门的都会,具体的解释可以看看《手把手教你用ASP做留言本》教程,
接下来这是分页中比较重要的部分,了了三行而已:
<%
rs.pagesize=5
curpage=Request.QueryString("curpage")
rs.absolutepage=curpage
%>
第二句:
rs.pagesize=5,这个什么意思呢?它就是在Recordset对象中的一个内置属性,它的作用是指定每页的记录条数,设置为5时,每5条记录放在一起成一页,比如实例*有21条记录,那么,使用rs.pagesize分页后,这21条记录将分成5页进行显示。
第三句:
这里主要是用于翻页的功能,将URL的post参数curpage传递给curpage变量,这个curpage将得到浏览者想要到达的页数。(运行一下实例就会明白)
第四句:
rs.absolutepage,这个也是个内置的属性,,它代表的意思就是将curpage变量的数值指定为当前页。
现在开始可以让记录循环显示了:
<%
for i= 1 to rs.pagesize
if rs.eof then
exit for
end if
%>
<%=rs("record_info")%><br>
<%
rs.movenext
next
%>
第二句:
利用for循环在每页显示rs.pagesize属性中指定的记录数。
第三、四、五句:
这句意思是当最后一页达不到指定记录时就退出循环,以免出错。
第七句:
绑定从数据库取出的record_info字段,就是叫这字段内的记录循环显示的。
第九句:
用rs.movenext方法将rs记录集往下移一条记录。
第十句:
for循环语句。
另外可以用<%=curpage%>读出当前页次,用<%=rs.pagecount%>读出总页数,用& lt;%=rs.recordcount%>读出总记录数。例如:“当前第<%=curpage%>页,共有& lt;%=rs.pagecount%>页,共有:<%=rs.recordcount%>条记录”。
在显示首页、上页、下页、尾页功能上,采用了if...else...语句,比较好懂。
<%if curpage=1 then%>
首页
<%else%>
<a href="?curpage=1">首页</a>
<%end if%>
<%if curpage=1 then%>
上一页
<%else%>
<a href="?curpage=<%=curpage-1%>">上一页</a>
<%end if%>
<%if rs.pagecount<curpage+1 then%>
下一页
<%else%>
<a href="?curpage=<%=curpage+1%>">下一页</a>
<%end if%>
<%if rs.pagecount<curpage+1 then%>
尾页
<%else%>
<a href="?curpage=<%=rs.pagecount%>">尾页</a>
<%end if%>
理解一下:
首页:
这个使用当前页是否为第一页时判别,如果当前为第一页(也就是首页),那么显示首页两字,没有链接,否则提供直接跳转到首页的链接。
上一页:
当前为第一页时,链接失效,反过来,链接到当前面的上一页,这里使用:<%=curpage-1%>,就是用当前的页数减去1,得到上一页。
下一页:
这里需要使用rs.pagecount这个属性来比较,假如总页数小于当前页数加1的值,那表明这就是第后一页,链接将失效,否则链接到下一页。
尾页:
和下一页的功能一样判定出是最后页时链接失效,否则将当前页指定为rs.pagecount(总页数)。

 

 

 

<!-- #i nclude file="../conn.asp" -->    //(Yoko:路径按自己的设置好)
<%
dim i,intPage,page,pre,last,filepath
  set rs = server.CreateObject("adodb.recordset")
   sql="select * from user order by user_ID desc"
      rs.PageSize = 20         //(Yoko:这里设定每页显示的记录数
      rs.CursorLocation = 3
      rs.Open sql,conn,0,2,1   //(Yoko:'这里执行你查询SQL并获得结果记录集
      pre = true
      last = true
      page = trim(Request.QueryString("page"))
     
      if len(page) = 0 then
                  intpage = 1
                  pre = false
      else
          if cint(page) =< 1 then
               intpage = 1
               pre = false
          else
              if cint(page) >= rs.PageCount then
                  intpage = rs.PageCount
                  last = false
              else
                   intpage = cint(page)
             end if
         end if
      end if
    if not rs.eof then
         rs.AbsolutePage = intpage
    end if
%>
<!--循环开始-->
 <%   
   for i=1 to rs.PageSize
     if rs.EOF or rs.BOF then exit for
  %>
      ..................
     //(Yoko:要循环显示的内容  )..................
      ...................
   <% 
     rs.movenext
    next
   %>
<!--循环体结束
分页部分:-->
 <table width="99%" border="1" cellpadding="2" cellspacing="2" borderColorLight=#808080 borderColorDark=#ffffff>
   <tr>
     <%if rs.pagecount > 0 then%>
     <td width="13%" align="left">当前页<%=intpage%>/<%=rs.PageCount%></td>
     <%else%>
    <td width="41%" align="left">当前页0/0</td><%end if%>
    <td width="46%" align="right"> <a href="本页.asp?page=1">首页</a>|
     <%if pre then%>
     <a href="本页.asp?page=<%=intpage -1%>">上页</a>| <%end if%>
     <%if last then%>
      <a href="本页.asp?page=<%=intpage +1%>">下页</a> |<%end if%>
      <a href="本页.asp?page=<%=rs.PageCount%>">尾页</a>|转到第
      <select name="sel_page" onchange="javascript:location=this.options[this.selectedIndex].value;">
      <%
       for i = 1 to rs.PageCount
       if i = intpage then%>
       <option value="本页.asp?page=<%=i%>" selected><%=i%></option>
     <%else%>
       <option value="本页.asp?page=<%=i%>"><%=i%></option>
        <%
          end if
        next
        %>
     </select>页</font>
    </td>
    </tr>
   </table>

 

 

下面是CSDN的:


<%

         dim Conn,RS,sqlStr,PageSize,RowCount,TotalPages,PageNo,Position,PageBegin,PageEnd

         set Conn= Server.CreateObject("ADODB.Connection")

         set RS = Server.CreateObject("ADODB.RecordSet")

         Conn.open "provider=sqloledb;data source=127.0.0.1;user id=sa;password=;initial catalog=zjydata"

         sqlStr="select * from Items order by iID"

         RS.open sqlStr,Conn,1,1

         PageSize=10

         If RS.RecordCount=0 then

         %>

                   <TABLE WIDTH=100% BORDER=0 CELLPADDING=1 CELLSPACING=1 style='font-size:9pt'>

                   <TR bgcolor=#003366 height=20px>

                   <TD align=center><font color=#FFFFFF>商品代码</font></TD>

                   <TD align=center><font color=#FFFFFF>商品名称</font></TD>

                   <TD align=center><font color=#FFFFFF>型号规格</font></TD>

                   <TD align=center><font color=#FFFFFF>计量单位</font></TD>

                   <TD align=center><font color=#FFFFFF>参考进价</font></TD>

                   <TD align=center><font color=#FFFFFF>参考售价</font></TD>

                   <TD align=center><font color=#FFFFFF>备注</font>

                   </TR>

                   <TR height=20px>

                   <TD align=center>没有任何商品信息!</TD>

                   <TR>

                   </TABLE>

         <%

         else

                   RS.PageSize = Cint(PageSize)

                   TotalPages=RS.PageCount

                   PageNo=Request.QueryString("PageNo")

                   if PageNo="" or PageNo<1 Then

                            PageNo = 1

                   end if

                   RS.AbsolutePage = PageNo

                  Position=RS.PageSize*PageNo

                   PageBegin=Position-RS.PageSize+1

                   if Position <RS.RecordCount then

                            PageEnd=Position

                   else

                            PageEnd= RS.RecordCount

                   end if

                   %>

                   <TABLE WIDTH=100% BORDER=0 CELLPADDING=1 CELLSPACING=1 style='font-size:9pt'>

                   <TR>

                   <TD width=20%>

                   <%

                            Response.Write "页:"& PageNo &" / "& TotalPages &" | 记录:"& PageBegin & " - " & PageEnd & " / " &RS.RecordCount

                   %>

                   </TD>

                   <TD align=right>

                   <%

                            if PageNo > 1 Then

                                     response.write "<a href=list.asp?PageNo=1>首页</a>&nbsp;<a href=list.asp?PageNo=" & (PageNo-1) & ">上页</a>&nbsp;"

                            end if

                            if  TotalPages > 1 and  cint(PageNo) <> cint(TotalPages) then

                                     response.write "<a href=list.asp?PageNo=" & (PageNo+1) & ">下页</a>&nbsp;<a href=list.asp?PageNo=" & TotalPages & ">末页</a>&nbsp;"

                            end if

                   %>

                   </TD>

                   </TR>

                   <TR bgcolor=#003366 height=20px>

                   <TD align=center width=20%><font color=#FFFFFF>商品代码</font></TD>

                   <TD align=center width=30%><font color=#FFFFFF>商品名称</font></TD>

                   <TD align=center width=8%><font color=#FFFFFF>型号规格</font></TD>

                   <TD align=center width=8%><font color=#FFFFFF>计量单位</font></TD>

                   <TD align=center width=8%><font color=#FFFFFF>参考进价</font></TD>

                   <TD align=center width=8%><font color=#FFFFFF>参考售价</font></TD>

                   <TD align=center width=18%><font color=#FFFFFF>备注</font></TR>

                   <%

                   RowCount = RS.PageSize

                   dim color1,color2

                   color1="#dddddd"

                   color2="#eeeeee"

                   Do While Not RS.EOF and RowCount > 0

                   Response.Write "<TR height=20px bgcolor="

                   %>

                   <%

                            if RowCount mod 2=0 then:response.write color1 else:response.write color2

                   %>

                   <%

                            Response.Write ">"

                   %>

                   <TD><span><a href=manage.asp?id=<%=rs(0)%>><% =RS("vCode")%></a></span></td>

                   <TD><span><% =RS("vName")%></span></td>

                   <TD><span><% =RS("vSpec")%></span></TD>

                   <TD><span><% =RS("vUnit")%></span></td>

                   <TD><span><% =RS("fInPrice")%></span></td>

                   <TD><span><% =RS("fOutPrice")%></span></TD>

                   <TD><span><% =RS("vDescription")%></span></td>

                   </TR>

                   <%

                   RowCount = RowCount - 1

                   RS.MoveNext

                   Loop

                   Conn.Close

                   set RS = nothing

                   set Conn = nothing

                   %>

                   <TR bgcolor=#003366 height=20px>

                   <TD align=center width=20%><font color=#FFFFFF>商品代码</font></TD>

                   <TD align=center width=30%><font color=#FFFFFF>商品名称</font></TD>

                   <TD align=center width=8%><font color=#FFFFFF>型号规格</font></TD>

                   <TD align=center width=8%><font color=#FFFFFF>计量单位</font></TD>

                   <TD align=center width=8%><font color=#FFFFFF>参考进价</font></TD>

                   <TD align=center width=8%><font color=#FFFFFF>参考售价</font></TD>

                   <TD align=center width=18%><font color=#FFFFFF>备注</font>

                   </TR>

                   <TR>

                   <TD>

                   <%

                            For i=1 to TotalPages

                                     if CInt(PageNo) = CInt(i) then

                                               response.write "["&i&"]"

                                     else

                                               response.write "&nbsp<a href=list.asp?PageNo="&i&">"&i&"</a>&nbsp"

                                     end if

                            Next

                   %>

                   </TD>

                   </TR>

                   </TABLE>

                   <%

         End if

%>