关于ASP的分页技术

来源:互联网 发布:58招淘宝客服是真的吗 编辑:程序博客网 时间:2024/04/29 23:24
      在web系统中,对于大批量的数据,单一的画面无法显示全部的数据,所以就需要使用分页显示技术。由于asp主要使用ADO连接数据库,所以ASP分页技术也主要涉及到ADO的Recordset 对象。
Recordset 对象主要关于分页的属性如下:
              (1)AbsolutePage属性 识别当前记录所在的页码 
              例如:rs.absolutePage=1
              (2)AbsolutePosition 属性 可根据其在 Recordset 中的序号位置移动到记录,或确定当前记录的序号位置。提供者必须支持该属性的相应功能才能使用该属性。                      
              (3)Pagecount属性 确定 Recordset 对象中数据的页数。“页”是大小等于 pagesize 设置的记录组。 该值为 -1 以表明 PageCount 无法确定。
              例如:totalpages = rs.pagecount
              (4)Pagesize 属性 可确定组成逻辑数据页的记录数。
              例如:pagesize = 10
              (5)Recordcount 属性 可确定 Recordset 对象中记录的数目。
              例如:total = rs.recordcount
 
 
 
一.导航样分页:
 举例说明:
<!--#include file="./include/db.asp"-->
<%
Dim sql
Dim page,pagesize
Dim total,totalpages
pagesize=2
page = Request("page")
if(page = "") then page = 1
 
sql = "select * from TBL1 order by age"
call SqlConnect(Sql)
rs.pagesize=pagesize
totalpages=rs.pagecount
total = rs.RecordCount
%>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<body>
 <table border=1 width="100%">
   <tr>
     <td>no</td>
     <td>id</td>
     
     <td>name</td>
     <td>age</td>
    </tr> 
    <%for i=1 to pagesize%>
    <tr>
     <td><%=(cint(page) - 1) * cint(pagesize) + i%></td>
     <td><%=rs("ID")%></td>     
     <td><%=rs("NAME")%></td>
     <td><%=rs("AGE")%></td>
    </tr> 
    <%rs.movenext
    if rs.eof then exit for
    next%>
      <%if(cint(page) <= cint(totalpages)) then%>      
            <td width="100%" colspan=13 align="right">共<%=totalpages%>页、当前第<%=page%>页
           <a href=test.asp?page=1><font color="#0000FF">首页</font></a>
           -<%if page > 4 then %><a href=test.asp?page=<%=page-4%>><font color="#0000FF"><%=page - 4%></font></a>-<%end if%>
           <%if page > 3 then %><a href=test.asp?page=<%=page-3%>><font color="#0000FF"><%=page - 3%></font></a>-<%end if%>
           <%if page > 2 then %><a href=test.asp?page=<%=page-2%>><font color="#0000FF"><%=page - 2%></font></a>-<%end if%>
           <%if page > 1 then %><a href=test.asp?page=<%=page-1%>><font color="#0000FF"><%=page - 1%></font></a>-<%end if%>
           <%=page & "-"%>
           <%if totalpages >= page + 1 then %><a href=test.asp?page=<%=page+1%>><font color="#0000FF"><%=page + 1%></font></a>-<%end if%>
           <%if totalpages >= page + 2 then %><a href=test.asp?page=<%=page+2%>><font color="#0000FF"><%=page + 2%></font></a>-<%end if%>
           <%if totalpages >= page + 3 then %><a href=test.asp?page=<%=page+3%>><font color="#0000FF"><%=page + 3%></font></a>-<%end if%>
           <%if totalpages >= page + 4 then %><a href=test.asp?page=<%=page+4%>><font color="#0000FF"><%=page + 4%></font></a>-<%end if%>
           <a href=test.asp?page=<%=totalpages%>><font color="#0000FF">末页</font></a>
            </td>
         </tr>       
      <%end if%>   
  </table>  
</body>
<%call SqlDisConnect%>
效果:
 
二.一般分页:
举例说明:
<!--#include file="./include/db.asp"-->
<%
Dim sql
Dim page,pagesize
Dim total,totalpages

pagesize=4
page = Request("page")

if(page = "" ) then page = 1 
 
sql = "select * from TBL1 order by age"
call SqlConnect(Sql)
rs.pagesize=pagesize
totalpages=rs.pagecount
total = rs.RecordCount

if cint(page)<1 then
    page = 1 
end if
if cint(page)>cint(totalpages) then
    page = totalpages 
end if

%>

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<body>
<form method=post action="test.asp" name ="myform">
 <table border=1 width="100%">
   <tr>
     <td>no</td>
     <td>id</td>
     
     <td>name</td>
     <td>age</td>
    </tr> 
    <%for i=1 to pagesize%>
    <tr>
     <td><%=(cint(page) - 1) * cint(pagesize) + i%></td>
     <td><%=rs("ID")%></td>     
     <td><%=rs("NAME")%></td>
     <td><%=rs("AGE")%></td>
    </tr> 
    <%rs.movenext
    if rs.eof then exit for
    next%>
      <tr>     
          <td width="100%" colspan=13 align="right">共<%=totalpages%>页、当前第<%=page%>页
          <%if cint(page)=1 then%>
             首页
          <%else%> 
            <a href=test.asp?page=1><font color="#0000FF">首页</font></a>
         <%end if%>  
         <%if cint(page)=1 then%>
           上页
         <%else%> 
           <a href=test.asp?page=<%=page-1%>><font color="#0000FF">上页</font></a>
         <%end if%>
          <%if cint(page)=cint(totalpages) then%>
             下页
         <%else%>
            <a href=test.asp?page=<%=page+1%>><font color="#0000FF">下页</font></a>
         <%end if%>
         <%if cint(page)=cint(totalpages) then%>
             末页
         <%else%>
            <a href=test.asp?page=<%=totalpages%>><font color="#0000FF">末页</font></a>
         <%end if%>
         <input type="text" name="page" size="4" >          
         </td>                                                          
     </tr>             
  </table>  
</form> 
</body>
<%call SqlDisConnect%>
原创粉丝点击