Asp 高效分页代码,带注释

   日期:2017-06-15

<SCRIPT LANGUAGE=javascript>
<!--
function sss(i)
{
document.s.pagelabel.value =i;
document.s.submit();

}
//-->
</SCRIPT>

<%


mysql="select count(*) from youtableName "
rs.open mysql,conn,1
totalcount=rs(0) '总记录数
rs.close

pagesize=30 '每页显示记录数
pagelabel=clear(request("pagelabel")) '第pagelabel页
if pagelabel="" then
pagelabel=1
end if

'***********************下面是关键,我们每页显示pagesize条数据,我们select的时候就只选择pagesize数量的记录。

if pagelabel<>1 then '如果不是第一页,假设是第3页,那么你需要知道前(pagelabel-1)*pagesize条记录最大的ID(该ID是表自增加ID索引)
mysql="select max(id) from (select top "&(pagelabel-1)*pagesize&" id from youtableName " )
mysql=mysql&") ta" 
rs.open mysql,conn,1
pagelabel=rs(0) 
rs.close() 
mysql="select top "&pagesize&" * from youtableName where id>"&pagelabel '取出大于ID的pagesize条记录
else
mysql="select top "&pagesize&" * from youtableName " 
end if


set rs=conn.Execute(mysql)
%>
<table border=0 cellpadding=0 cellspacing=0 width="100%" class="big">
<form name="s" action="" method="post">
<tr class="head">
<td align='center'>列名1</td>
<td align='center'>列名2</td>
<td align='center'>列名3</td>
</tr>
<input type="hidden" name="pagelabel">
</form>
<%
if not rs.eof then
arrdata1=rs.GetRows()
rs.close
set rs=nothing

rowsA=ubound(arrdata1,2) '本页的记录数

pagecount=totalcount / pagesize '页数
if ((totalcount mod pagesize)<>0) then
pagecount=int(pagecount)+1
else
pagecount=int(pagecount)
end if
for i=0 to rowsA 
Response.Write "<TR><TD align=center>"&arrdata1(1,i)&"</td>"
Response.Write "<td align='center'>"&arrdata1(2,i)&"</td>"
Response.Write "<td align='center'>"&arrdata1(3,i)&"</td>"
'Response.Write "<td align='center'>"&arrdata1(4,i)&"</td>"
'Response.Write "<td align='center'>"&arrdata1(4,i)&"</td>"
Response.Write "</tr>"
Response.Write "<TR><TD colspan='5' height='1' bgcolor='#E6E7E4'></td></tR>"
next 
%>
<tr><td height="30" align="center">
<%
Response.write "总条数:<font color=red>"&totalcount&"</font></td><td colspan=4>&nbsp;&nbsp;分页</font>&nbsp;&nbsp;&nbsp;&nbsp;"
for i=1 to pagecount 
Response.Write "<a href='javascript:sss("&i&")' style='color:#398CE7'>"&i&"</a>&nbsp;&nbsp;"
next
%>
</td></tr> 
<%
else
Response.Write "<tr><td colspan=5 align=center>无纪录</td></tr>"
end if
%>
</table>