有时候,大家是不是因为写一些重复的代码而感到烦恼呢?由于在做ASP程序时,很多时候都要用到分页,重复作分页程序错误多不说了,而且影响到作程序的积极性,故写一分页函数,方便大家调用。如下:
<%
'================= ProgrammName: 分页函数 =======================
' ================ Programmed by Bubuy =====================
' ==================== 2003.10.16 ========================
Function cutPage(sqlStr,Conn,dateNums,pageNums,URLs)'利用ADO分页的函数
Dim sql,Cn,dateNum,pageNum,URL,rsDate
Sql=Trim(sqlStr) '获得sql语句。
Set Cn=Conn '获得数据对象
dateNum=Cint(dateNums) '获得每页得记录数
pageNum=Cint(pageNums) '获得当前页码
URL=Trim(URLs) '获得路径
Set rsDate=Server.CreateObject("ADODB.Recordset")
rsDate.PageSize=dateNum
rsDate.Open Sql,cn,1,1
IF rsDate.Eof Then
Response.Write("<center><font stlye='font-size:14px;' color='#ff0000'>对不起,没有记录!</font></center>")
Else
IF pageNum="" or pageNum<1 Then
pageNum=1
ElseIf pageNum>rsDate.PageCount Then
pageNum=rsDate.PageCount
End IF
rsDate.absolutepage =pageNum
Dim recordHead,recordLast '定义当前页开头记录和结束记录
recordHead=1
If pageNum>1 Then recordHead=dateNum*(pageNum-1)
If pageNum>=rsDate.PageCount Then
recordLast=rsDate.RecordCount
Else
recordLast=dateNum*pageNum
End If
Response.Write("<table width=100% border='0' cellpadding='0' cellspacing='0' style='font-size:12px;'>")
Response.Write("<tr><td height=38 align=left><font stly='font-size:12px;'>(第"&recordHead&"-"&recordLast&"条,共"&rsDate.Recordcount&"条,每页显示"&dateNum&"条)</font></td></tr>")
Dim URLa '定义判断输入得URL包含?没有的变量
Dim upPage,downPage,allPage '定义向上和向下翻的变量
Dim allWrite '定义输出
upPage=pageNum-1
downPage=pageNum+1
URLa=Split(URL,"?",-1,1)
If URLa(0)=URL Then
upPage="<a href=" & URL & "?page=" & upPage &" stlye='font-size:12px;'>上一页</a> "
If pageNum=1 Then upPage=""
downPage="<a href=" & URL & "?page=" & downPage &" stlye='font-size:12px;'>下一页</a>"
If pageNum=rsDate.PageCount Then downPage=""
Else
upPage="<a href=" & URL & "&page=" & upPage &" stlye='font-size:12px;'>上一页</a> "
If pageNum=1 Then upPage=""
downPage="<a href=" & URL & "&page=" & downPage &" stlye='font-size:12px;'>下一页</a>"
If pageNum=rsDate.PageCount Then downPage=""
End If
allWrite=upPage & downPage & " 共" & rsDate.PageCount & "页 " & "目前第"& pageNum &"页"
allwrite="<font style='font-size:12px;'>" & allWrite & " 到第<input type='text' style='width:30px;' Name='page'>页<input type='submit' value='GO'></font>"
Response.Write("<tr><form name='formPage' method='post' action="&URL&"><td height=30 align=right style='font-szie=12px;'>" & allWrite & "</td></form></tr>")
Response.Write("<tr><td align=center>")
Response.Write("<table width=100% border='0' cellpadding='0' cellspacing='0' style='font-size:12px;'>")
Response.Write("<tr>")
Response.Write("<tr bgcolor='#efffce' height=25>")
Dim id,i
For i=0 to rsDate.Fields.Count-1 '设置表头
Response.Write("<td align='center'><font style='font-size:14px;'><b>"&rsDate.Fields(i).Name&"</b></font></td>")
Next
Response.Write("</tr>")
id=0
While not rsDate.EOF and id<dateNum
id=id+1
If id Mod 2=0 then
Response.Write("<tr bgcolor=#f7f6e7>")
Else
Response.Write("<tr bgcolor=#ffffff>")
End If
For Each fils in rsDate.Fields
Response.Write("<td align='center' height=20>"&fils&"</td>")
Next
Response.Write("</tr>")
rsDate.MoveNext
Wend
Response.Write("<tr height=25 bgcolor='#efffce'>")
For i=0 to rsDate.Fields.Count-1 '设置表尾
Response.Write("<td align='center'><font style='font-size:14px;'><b>"&rsDate.Fields(i).Name&"</b></font></td>")
Next
Response.Write("</tr>")
Response.Write("</table></td></tr>")
Response.Write("<tr><form name='formPage1' method='post' action="&URL&"><td height=30 align=right>" & allWrite & "</td></form></tr>")
End IF
rsDate.close
Set rsDate=nothing
End Function
%>
这样使用
page=request("page")
sql="select * from [table]" '写sql语句
url="xx.asp"当然也可以是"xx.asp?bubuy=1"
conn大家都知道是定义的什么吧
调用就这样
Call cutPage(sql,conn,"3",page,url)'3代表每页显示3条记录
由于我水平有限,程序中难免有错,大家顺便提提意见。大家也可以添加新得功能或修改已有得功能不过别忘了改后给我一份噢,谢谢,bubuy@sohu.com