<% 'ASP通用分页类 '江鸿宾(QQ33080907) '最近修改:2020.12.09 '本类只用于作者参与的项目,未经许可请勿转载 '定义导航按钮的样式 Const Btn_First = "9" Const Btn_Prev10 = "7" Const Btn_Prev = "3" Const Btn_Next = "4" Const Btn_Next10 = "8" Const Btn_Last = ":" '定义页面跳转输入框和Go按钮的样式 Const Ipt_Style = "font-family: Verdana; font-size: 10px; height: 15px; border: 1px solid #666666" '类代码的开始标志 Class SplitPage '定义私有变量 Private PageCount, myCon, myRs, mySQL, CurrentPage, TotalNumber, URL, Params, FileName, myPageSize, myStyle '显示页面信息,可以根据需要修改 Private Function ShowPageInfo() Dim str_tmp str_tmp = "共" & TotalNumber & "条记录 每页" & myPageSize & "条 这是第" & CurrentPage & "/" & PageCount & "页" ShowPageInfo = str_tmp End Function '页码跳转输入框 Private Function Page_Input() Page_Input = " " End Function '页码跳转Go按钮 Private Function Page_Button() Page_Button = " " End Function '页码跳转选择列表 Private Function Page_Select() Dim str_tmp, i str_tmp = " 转到第页" Page_Select = str_tmp End Function '显示翻页(myStyle表示导航栏样式1~8) Public Sub ShowPage() Dim i, wrapstr1,wrapstr2,pagestr if TotalNumber>0 then wrapstr1 = "
" & ShowPageInfo & "
" If PageCount > 1 Then '调用各个分页样式 Select Case myStyle Case 1: pagestr = PageStyle1 Case 2: pagestr = PageStyle2 Case 3: pagestr = PageStyle3 Case 4: pagestr = PageStyle4 Case 5: pagestr = PageStyle5 Case 6: pagestr = PageStyle6 Case 7: pagestr = PageStyle7 Case 8: pagestr = PageStyle8 End Select End If wrapstr2 = "
" end if '手机端特殊处理,不要外层包含元素 if myStyle>=6 then Response.Write pagestr else Response.Write wrapstr1 & pagestr & wrapstr2 end if End Sub '数据显示页面通过该函数取得Rs对象,然后自定义显示方式 Public Function Rs() On Error Resume Next Set myRs = Server.CreateObject("ADODB.RecordSet") myRs.PageSize = myPageSize myRs.Open mySQL, myCon, 1 If err then Response.Write "
数据库查询出错," & err.description & "

" Err.Clear response.end end if If Not myRs.EOF Then '记录总数 TotalNumber = myRs.RecordCount '页面总数 PageCount = myRs.PageCount '当前输出页码 If CurrentPage > PageCount Then CurrentPage = PageCount myRs.AbsolutePage = CurrentPage End If Set Rs = myRs End Function '得到数据库连接对象 Public Property Let Conn(oCon) Set myCon = oCon End Property '得到查询语句 Public Property Let SQL(str) mySQL = str End Property '得到分页的样式(1~8) Public Property Let Style(num) If IsNumeric(num) Then myStyle = CInt(num) If myStyle < 1 Or myStyle > 8 Then myStyle = 1 Else myStyle = 1 End If End Property '每一页的记录条数,页传递给类 Public Property Let PageSize(num) If IsNumeric(num) Then myPageSize = CInt(num) If myPageSize < 1 Then myPageSize = 10 Else myPageSize = 10 End If End Property '数据显示页面中每一页输出的条数,类传递给页 Public Property Get PageSize() '确定每页输出的条数,照顾到最后一页记录数不足的情况 If CurrentPage = PageCount Then PageSize = TotalNumber - myPageSize * (PageCount - 1) Elseif TotalNumber=0 then pagesize=0 else PageSize = myPageSize end if End Property '类的初始化 Private Sub Class_Initialize() dim TmpUrl '初始化每页显示记录条数为10 myPageSize = 10 '初始化分页样式为1 myStyle = 1 '初始化数据库连接对象 Set myCon = Con '处理传入的Page参数 CurrentPage = Trim(Request.QueryString("Page")) If IsNumeric(CurrentPage) Then CurrentPage = CLng(CurrentPage) If CurrentPage < 1 Then CurrentPage = 1 Else CurrentPage = 1 End If '得到当前ASP页面的文件名 FileName = Trim(Request.ServerVariables("URL")) FileName = Mid(FileName, InStrRev(FileName, "/") + 1) '取得当前文件的查询字符串(即所带参数)转换为数组 Params = Split(Trim(Request.ServerVariables("QUERY_STRING")), "&") '取得当前页完整URL,提供给页码链接使用(GetURL函数定义在后面) URL = Server.HtmlEncode(GetURL) TmpUrl=lcase(URL) if instr(TmpUrl,"%3ciframe")>0 or instr(TmpUrl,"%3cscript")>0 or instr(TmpUrl,"'")>0 or instr(TmpUrl,"javascript")>0 or instr(TmpUrl,"expression")>0 then Call RedirectMessage("对不起,您的地址栏含有非法字符!!!") End Sub '每一条记录的序号,跟页码相关,例如第2页第一条是在第一页最后序号基础上加1 Public Function ListNum(num) ListNum = myPageSize * (CurrentPage - 1) + num End Function '取得当前URL,页面间跳转保持参数 Private Function GetURL() Dim i, str For i = 0 To UBound(Params) If InStr(LCase(Params(i)), "page=") = 0 Then str = str & "&" & Params(i) Next str = Mid(str, 2) If str = "" Then GetURL = FileName & "?" & "page=" Else GetURL = FileName & "?" & str & "&page=" End Function '分页样式1:列出所有页码的页号,页码较少时可以使用 Private Function PageStyle1() Dim str str = "转到 第" For i = 1 To PageCount If i = CurrentPage Then str = str & "" & i & " " Else str = str & "" & i & " " Next str = str & "页" PageStyle1 = str End Function '分页样式2:显示首页、上一页、下一页、末页,均为符号和转到页的输入框 Private Function PageStyle2() Dim str If CurrentPage > 1 Then str = str & "" & Btn_First & " " str = str & "" & Btn_Prev & " " Else str = str & Btn_First & " " & Btn_Prev End If If CurrentPage < PageCount Then str = str & "" & Btn_Next & " " str = str & "" & Btn_Last & " " Else str = str & Btn_Next & " " & Btn_Last End If '显示页码输入框,如果不显示则注释掉 str = str & Page_Input '显示Go字样的按钮,如果不显示则注释掉 str = str & Page_Button PageStyle2 = str End Function '分页样式3:显示首页、上十页、下十页、尾页以及中间一段页码 Private Function PageStyle3() Dim str, p p = (CurrentPage - 1) \ 10 '首页 If CurrentPage = 1 Then str = str & Btn_First & " " Else str = str & "" & Btn_First & " " '上十页 If p * 10 > 0 Then str = str & "" & Btn_Prev10 & " " '页码 For i = (p * 10 + 1) To (p * 10 + 10) If i = CurrentPage Then str = str & "" & i & " " Else str = str & "" & i & " " If i = PageCount Then Exit For Next '下十页 If i < PageCount Then str = str & "" & Btn_Next10 & " " '尾页 If CurrentPage = PageCount Then str = str & Btn_Last Else str = str & "" & Btn_Last & "" PageStyle3 = str End Function '分页样式4:显示首页和尾页的页码、中间显示一部分页码 Private Function PageStyle4() Dim str, n, pageto, i If CurrentPage > 4 Then str = str & "[1] ..." If CurrentPage < PageCount - 3 Then pageto = CurrentPage + 3 Else pageto = PageCount For i = CurrentPage - 3 To pageto If i >= 1 Then If i = CurrentPage Then str = str & " [" & i & "]" Else str = str & " [" & i & "]" End If Next If CurrentPage < PageCount - 3 Then str = str & " ... [" & PageCount & "]" PageStyle4 = str End Function '分页样式5:显示首页、上一页、下一页、末页,均为汉字和转到页的选择列表 Private Function PageStyle5() Dim str If CurrentPage > 1 Then str = str & "首页 " str = str & "上一页 " Else str = str & "首页 上一页" End If If CurrentPage < PageCount Then str = str & " 下一页 " str = str & "末页 " Else str = str & " 下一页 末页" End If '显示页码的选择列表,如果不显示则注释掉 str = str & Page_Select PageStyle5 = str End Function '分页样式6:显示上一页、下一页,当前页、当前页的前后两页,专供jquery.mobile.js准备数据 Private Function PageStyle6() Dim str if currentpage>=2 then str = str & "Prev" str = str & "" & CurrentPage-1 & "" end if str = str & "" & CurrentPage & "" if pagecount>currentpage then str = str & "" & CurrentPage+1 & "" str = str & "Next" end if PageStyle6=str End Function '分页样式7:显示上一页、下一页,当前页、当前页的前后两页,专供bootstrap准备数据 Private Function PageStyle7() Dim str str="" PageStyle7=str End Function '分页样式8:显示上一页、下一页,当前页、当前页的前后两页,专供AmazeUI准备数据 Private Function PageStyle8() Dim str str="" PageStyle8=str End Function '类结束标志 End Class %>