asp分页系统
asp分页系统
分页程序
'定义分页全局变量
dim code,mdbsql,Page_size,Totalrec,TotalPage,currentPage
![](/Images/OutliningIndicators/None.gif)
'参数处理,并把相应代码插入code(分页)和mdbsql(数据库查询)中
Sub s_keyword(s_value)
keyword = s_value '建议先对keyword进行处理
if keyword<>"" then
code = code &"&keyword="& keyword
text=Split(keyword) '分词
for each word in text
mdbsql = mdbsql &" and charindex('"&trim(word)&"',hw_area)>0"
next
end if
end Sub
![](/Images/OutliningIndicators/None.gif)
'sql搜索主程序,页次,查找字段,关键字段(唯一索引),排序字段
Sub fSql(s_page,s_table,s_str,s_key,s_order,mdbconn)
if s_page <> "" and IsNumeric(s_page) then currentPage = cint(s_page) else currentPage=1
if mdbsql = "" then mdbsql = "1=1"
Totalrec = mdbconn.execute("select count(*) from ["&s_table&"] where "&mdbsql)(0)
if Totalrec > 0 then
TotalPage = Totalrec\Page_size
If (Totalrec Mod Page_size) Then TotalPage = TotalPage+1
If TotalPage < 1 Then TotalPage = 1
If currentPage > TotalPage Then currentPage = TotalPage
If TotalPage = 1 then Page_size=Totalrec
![](/Images/OutliningIndicators/None.gif)
if currentPage = 1 then
mdbsql = "select top "&Page_size&" "&s_str&" from ["&s_table&"] where "&mdbsql&" order by "&s_order&" desc"
else if s_order <> "" and s_order <> s_key then
mdbsql = "select top "&Page_size&" "&s_str&" from ["&s_table&"] where "&mdbsql&" and "&s_key&" not in (select top "&(currentPage-1)*Page_size&" "&s_key&" from ["&s_table&"] where "&mdbsql&" order by "&s_order&" desc) order by "&s_order&" desc"
else
mdbsql = "select top "&Page_size&" "&s_str&" from ["&s_table&"] where "&mdbsql&" and "&s_key&" < (select min ("&s_key&") from (select top "&(currentPage-1)*Page_size&" info_id from ["&s_table&"] where "&mdbsql&" order by "&s_key&" desc) as T) order by "&s_key&" desc"
end if
end if
end Sub
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
'分页显示
Function show_page()
temp_page = ""
If currentPage <> 1 Then temp_page = temp_page & "<a href='?page=1"&code&"' title='第一页'><<-</a>" Else temp_page = temp_page & "<<-"
temp_page = temp_page & " "
If currentPage > 1 Then temp_page = temp_page & "<a href='?page=" & currentPage - 1 &code&"&' title='前一页'><-</a>" Else temp_page = temp_page & "<-"
temp_page = temp_page & " "
PageStart = currentPage - 5
If PageStart < 1 Then PageStart = 1
PageEnd = PageStart + 10
If PageEnd > TotalPage Then PageEnd = TotalPage
For Temp = PageStart To PageEnd
If Temp = currentPage Then temp_page = temp_page & "[" & Temp & "] " Else temp_page = temp_page & "<a href='?page="&Temp&""&code&"' title='第"&Temp&"页'>"&Temp&"</a> "
Next
If currentPage < TotalPage Then temp_page = temp_page & "<a href='?page=" ¤tPage + 1 & ""&code&"' title='下一页'>-></a>" Else temp_page = temp_page & "->"
temp_page = temp_page & " "
If currentPage < TotalPage Then temp_page = temp_page & "<a href='?page=" & TotalPage &code&"' title='最后一页'>->></a>" Else temp_page = temp_page & "->>"
temp_page = temp_page & " 共 "&Totalrec&" 条记录,当前第 "¤tPage&" 页,共 "&TotalPage&" 页 "
temp_page = temp_page & " 到第<input name='gopage' id='gopage' type='text' size='3' maxlength='6' value='"¤tPage&"' />页 <input type='button' value='go' onclick=""location.href='?page='+gopage.value+'"&code&"'"" />"
show_page = temp_page & ""
end Function
'以上程序放到一个function中
使用前定义好变量
'定义参数全局变量
dim keyword
![](/Images/OutliningIndicators/None.gif)
Page_size = 20
mdbsql="flag = 1" '可以先定义固定的搜索条件
![](/Images/OutliningIndicators/None.gif)
'函数会根据参数自动调整code和mdbsql
Call s_keyword(trim(Request("keyword")))
Call s_sql(trim(Request("page")),"title","id","id")
'以上代码一般放在程序开头
使用
'内容显示
if Totalrec > 0 then
set rs=server.createobject("adodb.recordset")
rs.open mdbsql,toconn,1,1
If rs.bof and rs.Eof Then
response.Write("暂时没有信息")
else
For Temp_FN = 1 to Page_size
If rs.Eof Then Exit For
response.write rs("title")
rs.movenext
next
response.write show_page() '分页显示
end if
else
response.Write("暂时没有信息")
end if