提供一点小建议,假如每页10条纪录,那么第一页就显示select top(10) * from [tablename] where ...,第二页取20条出来分页select top(20) * from [tablename] where ...,如此类推select top(n*10) * from [tablename] where ...,这样的效率会高很多,就算数据十分庞大,数据库一次才取10条,20条纪录来分页,况且浏览的时候一般看3~5页基本上就解决问题了,大家评论吧,我就是这样节省服务器的资源的
Private Sub Class_Initialize()
int_PageSize = 10
str_TitleColor = "#ffffff"
str_Error = ""
End Sub
'===============================================================
'属性信息
'================================================================
'-----------------------------------
'数据源,暂时只支持RecordSet
'-----------------------------------
Public Property Let dataSource(obj)
set obj_RecordSet = obj
End Property
Public Property Let pageSize(intValue)
int_PageSize = intValue
End Property
Public Property Get pageSize
PageSize= int_Categoryid
End Property
Public Property Let Fields(Arr)
Arr_Field = Arr
End Property
Public Property Get Fields
Fields= Arr_Field
End Property
Public Property Let fieldsName(Arr)
Arr_FieldName = Arr
End Property
Public Property Get fieldsName
fieldsName= Arr_FieldName
End Property
Public Property Let fieldWidth(Arr)
Arr_FieldWidth = Arr
End Property
Public Property Get fieldWidth
fieldWidth= Arr_FieldWidth
End Property
Public Property Let titleColor(strValue)
str_TitleColor = strValue
End Property
Public Property Get titleColor
titleColor= str_TitleColor
End Property
'-----------------------------------------------------
'这个属性是为了保存Url路径
'如果当前路径带有参数,那么就用&Page=x,否则就用?Page=x
'------------------------------------------------------
Public Property Let Url(StrValue)
str_Url = StrValue
End Property
Public Property Get Url
Url= str_Url
End Property
'================================================================
'方法
'================================================================
'----------------------------------------------------------------
'显示当前错误
'----------------------------------------------------------------
Private Sub ShowLastError()
response.Write(str_Error)
response.End()
End Sub
'----------------------------------------------------------------
'Generate()
'利用ado分页
'-----------------------------------------------------------------
Public Sub Generate()
'----检查参数--------------------------
Check
'---------变量声明-----------------------------------
Dim FieldCount '显示字段
FieldCount = Ubound(Arr_Field) + 1
Dim CurrentPage '当前页
Dim PgCount '总页数
Dim RecCount '记录数,本来用rs.recordCount可以取到,保存下来效率会比较高
Dim HasOtherParam 'URL是否包含其他参数
Dim PageParam '当前分页Url参数
Dim PageInfomation '当前分页状态信息
Dim Seperator '设置分隔符
Seperator = " "
'-------------处理Url参数---------------------------
if instr(str_Url,"?")>0 then
HasOtherParam = true
PageParam = "&Page="
else
HasOtherParam = false
PageParam = "?Page="
end if
'----------获取当前页--------------------------------
CurrentPage = request.QueryString("Page")
if CurrentPage="" then
CurrentPage=1
else
CurrentPage=Cint(CurrentPage)
end if
'-----------处理数据源------------------------------
obj_RecordSet.PageSize = int_PageSize
RecCount = obj_RecordSet.RecordCount
PgCount = obj_RecordSet.PageCount
IF obj_RecordSet.Eof Then
Response.Write("<center><font stlye='font-size:14px;' color='#ff0000'>对不起,没有记录!</font></center>")
Else
'-----------处理ADO分页----------------------------
IF CurrentPage < 1 Then
CurrentPage = 1
Else
If CurrentPage>PgCount Then
CurrentPage = PgCount
End If
End IF
obj_RecordSet.absolutepage = CurrentPage
Response.Write("<table width=100% border='0' cellpadding='0' cellspacing='0' style='font-size:12px;'>")
'---------------翻页链接-----------------------------
Dim FirstLink,PrevLink,NextLink,LastLink '定义向上和向下翻的变量
'-----------------------首页-------------------------
if CurrentPage>1 then
FirstLink = "<a href='" & URL & PageParam & "1'>首页</a>"
PrevLink = "<a href='" & URL & PageParam & Cstr(CurrentPage-1) & "'>上一页</a>"
else
FirstLink = "首页"
PrevLink = "上一页"
end if
'------------下一页----------------
if CurrentPage<PgCount then
NextLink = "<a href='" & URL & PageParam & Cstr(CurrentPage+1) & "'>下一页</a>"
LastLink = "<a href='" & URL & PageParam & PgCount & "'>尾页</a>"
else
NextLink = "下一页"
LastLink = "尾页"
end if
PageInfomation = FirstLink & Seperator & PrevLink & Seperator & NextLink & Seperator & LastLink & Seperator & "每页" & Cstr(int_PageSize) & "条记录" & Seperator & "共" & PgCount & "页" & Seperator & "目前第" & CurrentPage & "页" & Seperator
Response.Write("<tr><td align=center>")
Response.Write("<table width='100%' border='1' cellpadding='2' cellspacing='2' bordercolor='#999999'>")
'---------------设置表头-----------------
Response.Write("<tr bgcolor='" & str_TitleColor & "'>")
Dim i
For i=0 to FieldCount -1
Response.Write("<td align='center' width='" & Arr_FieldWidth(i) & "'><font style='font-size:14px;'><b>" & Arr_FieldName(i) & "</b></font></td>")
Next
Response.Write("</tr>")
'---------------------输出内容---------------------------------
i=0
While (not obj_RecordSet.EOF) and i<int_PageSize
Dim Cursor
Response.Write("<tr>")
For Cursor = 0 to FieldCount -1
Response.Write("<td align='center'>" & obj_RecordSet(Arr_Field(Cursor)) & "</td>")
Next
Response.Write("</tr>")
i=i+1
obj_RecordSet.MoveNext
Wend
'------------------------输出分页条------------------------------------
Response.Write("<tr><td align='right' colspan='" & Cstr(FieldCount) & "'>" & PageInfomation & "</td></tr>")
response.Write("</table></td></tr></table>")
End IF
End Sub
'----------检查参数是否正确---------------
Private Sub Check()
if Ubound(Arr_Field)<>Ubound(Arr_FieldName) then
str_Error="Fields数组和FieldName数组维数必须相同"
end if
if obj_RecordSet=empty then
str_Error="数据源不能为空,请设置dataSource属性"
end if
if int_PageSize="" then
str_Error="数据源不能为空"
end if
ShowLastError
End Sub
End Class
<%
'================= 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;'>页<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'>")
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=30 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