I will post the source code of the paging class first, and then I will post some examples to explain its usage and how to extend its functions when I have time.
The original code of the class is as follows:
< %
'==============================================
'XDOWNPAGE ASP version
'Current version: 2.0
'
'
'Original version 1.00
'Code by zykj2000
'Email: [email protected]
'BBS: http://bbs.513soft.net
'
'
'Upgrade version: 1.5 (asp + oracle)
updated by double
Email:
blog: ttp://blog.csdn.net/double/
'Upgrade instructions:
'
'
'Upgrade version: 2.0' (asp + oracle) ---->Current version
'Keep the original name: XDOWNPAGE
Updated by northsnow
'email: [email protected]
'blog: http://blog.csdn.net/precipitant
'Upgrade instructions:
'1, when querying data, only the records contained in the current page are queried, which greatly reduces the amount of data transmission.
'2, if the page navigation is normal, you don't need to query the total number of records every time, as long as you query it for the first time and then pass it through parameters.
'3, supports dynamically changing page size
'4, supports dynamic sorting
'5, this program only supports oracle, if you want to use it in sqlserver or other types of databases, please modify it yourself: Public Property Let GetSQL(str_sql).
'
'
'For other program modifier information, please check it in the source code! ! !
'This program can be used, modified, copied, reproduced, and quoted for free. I hope my program can bring convenience to your work.
'But please keep the above information, especially the original information. In addition, if it is used for commercial purposes, please upgrade with the original work and this version.
'Contact for permission.
'
'
'Program features
'This program mainly encapsulates the data paging part, and the data display part is completely customized by the user.
'Support multiple URL parameters
'
'Instructions for use
'Program parameter description
'PapgeSize defines the number of records on each page of paging
'GetRS returns the paginated Recordset. This property is read-only.
'GetConn gets the database connection
'GetSQL gets the query statement
'totalRecordCount passes the total number of records
' program method description
'ShowPage displays the paging navigation bar, the only public method
'ShowPageSizeChange() displays a list of changed page sizes
'
'example:
'
' 'Include files
'
'Set mypage=new xdownpage 'Create object
'mypage.getconn=conn 'Get the database connection
' mypage.getsql="select * from productinfo order by id asc"
' mypage.pagesize=5 'Set the record data of each page to 5
'mypage.totalRecordCount=rsTotalCount sets the total number of records
'set rs=mypage.getrs() 'Return Recordset
'mypage.GetSubmitForm="frmQuery" 'The form submitted by default for paging, currentpage parameter
'Response.write(mypage.GetSubmitForm1()) 'Output paging submission function
' mypage.showpage() 'Display paging information. This method can be used after set rs=mypage.getrs()
'Call anywhere, can be called multiple times
' do while not rs.eof 'The next operation is the same as operating an ordinary Recordset object.
' response.write rs(0) & "
' " 'Here you can customize the display method
'rs. movenext
' loop
'
'Added submission script to save current page number
'The function is GetSubmitForm()
'Need to submit a form name to the function GetSubmitForm
'Save the four parameters of variables flag, currentpage, pagesize, rsTotalCount in this submitted form
'Example is as follows
'flag=request("flag")
'currentpage=request("currentpage")
'currentpage=request("pagesize")
'currentpage=request("rsTotalCount")
'Add the following four inputs to the submitted form
'<input name="flag" type="hidden" value="< % =flag% >">
'<input name="currentpage" type="hidden" value="< % =currentpage% >">
'<input name="pagesize" type="hidden" value="< % =pagesize% >">
'<input name="rsTotalCount" type="hidden" value="< % =rsTotalCount% >">
'==============================================
Const Btn_First= "<font face=""webdings"">9</font>" 'Define the button display style on the first page
Const Btn_Prev="<font face=""webdings"">3</font>" 'Define the previous page button display style
Const Btn_Next="<font face=""webdings"">4</font>" 'Define the next page button display style
Const Btn_Last="<font face=""webdings"">:</font>" 'Define the last page button display style
Const XD_Align="center" 'Define the alignment of paging information
Const XD_Width="100%" 'Define the size of the paging information box
Const XD_Height="20"
Class Xdownpage 'Class starts here
' variable definition
public int_totalPage 'Total number of pages
public int_curcount 'Number of records on the current page
public XD_PageSize 'Page size
Private int_curpage 'Current page number
Private int_totalRecord 'Total number of records
Private XD_Conn 'Database connection object
Private XD_Rs 'Record set object
Private XD_SQL 'main sql statement
Private XD_Count_SQL 'SQL statement to query the total number of records
Private Str_errors
Private str_URL
Private XD_sURL
Private SubmitForm 'Required query form name (hide form name)
'================================================== ================
'PageSize property
'Set the paging size of each page
'================================================== ================
Public Property Let PageSize(int_PageSize)
If IsNumeric(Int_Pagesize) Then
if clng(Int_Pagesize)>0 then
XD_PageSize=CLng(int_PageSize)
else
XD_PageSize=10
end if
Else
XD_PageSize=10
End If
End Property
Public Property Get PageSize
If XD_PageSize="" or (not(IsNumeric(XD_PageSize))) Then
PageSize=10
Else
PageSize=XD_PageSize
End If
End Property
'================================================== ================
'GetRS property
'Return the paginated recordset
'================================================== ================
Public Property Get GetRs()
Set XD_Rs=Server.createobject("adodb.recordset")
'XD_Rs.PageSize=PageSize
XD_Rs.CursorLocation=3
XD_Rs.Open XD_SQL,XD_Conn,3,1
int_curcount=XD_Rs.RecordCount
if int_totalRecord="" or not isNumeric(int_totalRecord) then int_totalRecord=0 'Normalize the value of int_totalRecord
if int_totalRecord=0 and (int_curcount>=PageSize or int_curpage>1) then call queryRsCount() 'Query the total number of records
if err.number<>0 then
Response.Write err.Clear
end if
Set GetRs=XD_RS
End Property
'============================================== ==================
'queryRSCount method
'Query the total number of records
'================================================== ================
Public sub queryRsCount()
'The following code is used to calculate the total number of records
if XD_Count_SQL<>"" then
set rs_sqlcount=server.createobject("adodb.recordset")
rs_sqlcount.CursorLocation=3
rs_sqlcount.open XD_Count_SQL,conn,3,1
if (rs_sqlcount.eof and rs_sqlcount.bof) then
int_totalRecord=0
else
int_totalRecord=rs_sqlcount(0)
int_totalRecord=clng(int_totalRecord)
end if
rs_sqlcount.close
set rs_sqlcount=nothing
end if
End sub
'================================================ =================
'GetConn gets the database connection
'
'================================================== ===============
Public Property Let GetConn(obj_Conn)
Set XD_Conn=obj_Conn
End Property
'================================================ =================
'GetSQL gets the query statement
'
'================================================== ===============
Public Property Let GetSQL(str_sql)
if (str_sql<>"") then
'Based on the given query statement, generate the final query statement (only take the content of the current page): suitable for Oracle database
XD_SQL=" select * from (select rownum r_n,temptable.* from ("
XD_SQL=XD_SQL&str_sql
XD_SQL=XD_SQL&" ) temptable ) where r_n between " & cstr((int_curpage -1) * XD_PageSize +1) & " and " & cstr(int_curpage * XD_PageSize)
'Query statement to query the total number of records
XD_Count_SQL="select count(*) from ("& str_sql & ")"
end if
End Property
'================================================== ===============
'GetSubmitForm property sets the form for query conditions
'
'================================================== ===============
Public Property Let GetSubmitForm(frmName)
SubmitForm=trim(frmName)
End Property
'============================================== =================
'GetSubmitForm1 method outputs the script required for paging navigation
'
'================================================== ===============
public sub GetSubmitForm1()
'Javascript function of page navigation
Response.Write " "+vrcrlf
Response.Write ("<Script language=""javascript"">") +vbcrlf
Response.Write " function generalSubmit(i)"+vbcrlf
Response.Write " {"+vbcrlf
Response.Write " document."&SubmitForm&".flag.value=""query1111111155555"";"+vbcrlf
Response.Write " document."&SubmitForm&".currentpage.value=i;"+vbcrlf
Response.Write " "&SubmitForm&".submit();"+vbcrlf
Response.Write " }"+vbcrlf
'javascript function to change page size
Response.Write " function changePageSize(ii)"+vbcrlf
Response.Write " {"+vbcrlf
Response.Write " document."&SubmitForm&".flag.value=""query1111111155555"";"+vbcrlf
Response.Write " document."&SubmitForm&".currentpage.value=1;"+vbcrlf
Response.Write " document."&SubmitForm&".pagesize.value=ii;"+vbcrlf
Response.Write " "&SubmitForm&".submit();"+vbcrlf
Response.Write " }"+vbcrlf
Response.Write ("</Script>")+vbcrlf
Response.Write " "+vrcrlf
end sub
'================================================ ===================
'totalRecordCount property
'About the total number of records
'
'================================================== =================
Public Property Let totalRecordCount(int_totalRecordCount)
If IsNumeric(int_totalRecordCount) Then
int_totalRecord=CLng(int_totalRecordCount)
End If
End Property
Public Property Get totalRecordCount
If not(int_totalRecord="" or (not(IsNumeric(int_totalRecord)))) Then
totalRecordCount=int_totalRecord
End If
End Property
'================================================== =================
'GetRecordCount method
'Return the current number of records
'
'================================================== =================
public function GetRecordCount()
GetRecordCount=int_totalRecord
end function
'================================================== =================
'Class_Initialize class initialization
'Initialize the value of the current page
'
'================================================== =================
Private Sub Class_Initialize
'========================
'Set default values for some parameters
'========================
' XD_PageSize=10 'Set the default value of paging to 10
'========================
'Get the current value
'========================
If Request("currentpage")="" Then
int_curpage=1
ElseIf not(IsNumeric(Request("currentpage"))) Then
int_curpage=1
ElseIf CInt(Trim(Request("currentpage")))<1 Then
int_curpage=1
Else
Int_curpage=CInt(Trim(Request("currentpage")))
End If
End Sub
'==============================================
'ShowPage creates a paging navigation bar
'There are homepage, previous page, next page, last page, and digital navigation
'
'==============================================
Public Sub ShowPage()
Dim str_tmp
XD_sURL = GetUrl()
' int_totalRecord=XD_Rs.RecordCount
If int_totalRecord<=0 Then
str_error=str_error & "The total number of records is zero, please enter data"
Call ShowError()
End If
If int_totalRecord="" then
int_TotalPage=1
Else
'modify by wls 041215 For the right pages display--------------
If int_totalRecord mod PageSize =0 Then
int_TotalPage = CLng(int_TotalRecord XD_PageSize * -1) *-1
Else
int_TotalPage = CLng(int_TotalRecord XD_PageSize * -1)*-1+1
End If
End If
If Int_curpage>int_Totalpage Then
int_curpage=int_TotalPage
End If
'============================================== =========
'Display paging information, each module changes the display position according to its own requirements
'================================================== =======
'response.write " "
str_tmp=ShowFirstPrv
response.write str_tmp
str_tmp=showNumBtn
response.write str_tmp
str_tmp=ShowNextLast
response.write str_tmp
str_tmp=ShowPageInfo
response.write str_tmp
Response.write " "
ShowGoto
End Sub
'==============================================
'ShowFirstPrv displays the homepage and previous page
'
'
'==============================================
Private Function ShowFirstPrv()
Dim Str_tmp,int_prvpage
If int_curpage=1 Then
str_tmp=Btn_First&" "&Btn_Prev
Elseif int_curpage=0 then
str_tmp=Btn_First&" "&Btn_Prev
else
int_prvpage=int_curpage-1
str_tmp="<a href=""#"" onclick=""javascript:generalSubmit('1')"" alt=""First Page"">" & Btn_First&"</a> <a href="" #"" onclick=""javascript:generalSubmit('"&int_prvpage&"')"" alt=""Previous page"">" & Btn_Prev&"</a>"
End If
ShowFirstPrv=str_tmp
End Function
'==============================================
'ShowNextLast next page, last page
'
'
'==============================================
Private Function ShowNextLast()
Dim str_tmp,int_Nextpage
If Int_curpage>=int_totalpage Then
str_tmp=Btn_Next & " " & Btn_Last
Else
Int_NextPage=int_curpage+1
str_tmp="<a href=""#"" onclick=""javascript:generalSubmit('"&int_nextpage&"')"" alt=""Nextpage"">" & Btn_Next&"</a> <a href= ""#"" onclick=""javascript:generalSubmit('"&int_totalpage&"')"" alt=""Last Page"">" & Btn_Last&"</a>"
End If
ShowNextLast=str_tmp
End Function
'End Function
'==============================================
'ShowNumBtn modified numerical navigation
'
'==============================================
Function showNumBtn()
Dim i,str_tmp,end_page,start_page
start_page=1
'add by sll 2005.05.20 int_curpage=0
if int_curpage=0 then
str_tmp=str_tmp&"0"
else
if int_curpage>1 then
start_page=int_curpage
if (int_curpage<=5) then
start_page=1
end if
if (int_curpage>5) then
start_page=int_curpage-2
end if
end if
end_page=start_page+5
if end_page>int_totalpage then
end_page=int_totalpage
end if
For i=start_page to end_page
strTemp=XD_sURL & CStr(i)
str_tmp=str_tmp & "[<a href=""#"" onclick=""javascript:generalSubmit('"&i&"')"">"&i&"</a>] "
Next
end if
showNumBtn=str_tmp
End Function
'==============================================
'ShowGoto page jump
'The page automatically jumps
'add by sll 2005.05.20
'==============================================
Private Function ShowGoto()
'response.write int_totalPage
diminti
if int_totalPage<=0 then
response.write "<select name='goto' disabled>"
Response.Write "<option value='0'>0</option>"
response.write "</select>"
else
response.write "<select name='goto' onchange='javascript:generalSubmit(this.value)'>"
for inti=1 to int_totalPage
Response.Write "<option value='"&inti&"'"
if cstr(inti)=cstr(int_curpage) then
response.write "selected"
end if
response.write" >"&inti&"</option>"
next
response.write "</select>"
end if
End Function
'==============================================
'ShowPageInfo paging information
'Modify as required
'
'==============================================
Private Function ShowPageInfo()
Dim str_tmp
str_tmp=" [Page:<font color=red>"&int_curpage&"</font>/"&int_totalpage&"] [Total "&int_totalrecord&" items] ["&XD_PageSize&" items/page]"
ShowPageInfo=str_tmp
End Function
'==============================================
'ShowPageSizeChange changes the page size
'Modify as required
'
'==============================================
public sub ShowPageSizeChange()
Dim str_tmp
str_tmp="Page size: <select name='sssssPageSize' onchange='changePageSize(this.value)'>"
str_tmp=str_tmp & "<option"
if XD_PageSize=10 then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='10'>10</option>"
str_tmp=str_tmp & "<option"
if XD_PageSize=20 then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='20'>20</option>"
str_tmp=str_tmp & "<option"
if XD_PageSize=50 then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='50'>50</option>"
str_tmp=str_tmp & "<option"
if XD_PageSize=int_totalRecord then str_tmp =str_tmp & " selected "
str_tmp=str_tmp & " value='" & int_totalRecord & "'>all</option>"
str_tmp=str_tmp & "</select>"
response.Write str_tmp
End sub
'==============================================
'Modified function to get the current Url parameters
'Codeing by Redsun
'northsnow comment: don't know where to use it, but keep it
'==============================================
Private Function GetUrl()
Dim ScriptAddress, M_ItemUrl, M_item
ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))&"?" 'Get the current address
If (Request.QueryString <> "") Then
M_ItemUrl = ""
For Each M_item In Request.QueryString
If InStr("page",M_Item)=0 Then
M_ItemUrl = M_ItemUrl & M_Item &"="& Server.URLEncode(Request.QueryString(""&M_Item&"")) & "&"
End If
Next
ScriptAddress = ScriptAddress & M_ItemUrl 'Get the address with parameters
End If
GetUrl = ScriptAddress '& "page="
End Function
'==============================================
'Set the Terminate event.
'==============================================
Private Sub Class_Terminate
'XD_RS.close
'Set XD_RS=nothing
End Sub
'==============================================
'ShowError error message
'==============================================
Private Sub ShowError()
If str_Error <> "" Then
Response.Write("" & SW_Error & "")
Response.End
End If
End Sub
End class
%>