Asp 翻页是web开发必须的一个功能,下面代码是翻页功能的一个公共方法,可以在每个页面中直接引入。
此页面会显示一个翻页的功能条。此功能使用了一个page的变量来保存当前页号,使用时请注意不要修改。
<script language="javascript">
function JumpPage(thispage){
document.forms[0].action = thispage;
document.forms[0].submit();
}
</script>
<%
'获取页面数量
function GetPage()
GetPage = request("Page")
if GetPage = "" OR GetPage<1 then
GetPage = 1
else
GetPage = cint(GetPage)
end if
end function
'设置 翻页
function SetAbsPage(ByRef RetRs)
if RetRs is nothing then
SetAbsPage = false
exit function
end if
if RetRs.Recordcount <= 0 then
SetAbsPage = true
exit function
end if
Dim iPageSize
iPageSize =Cint(10) '每页记录数量,可修改
RetRs.PageSize = Cint(iPageSize)
iAbsPage = Cint(GetPage())
if iAbsPage > RetRs.PageCount then iAbsPage = RetRs.PageCount
RetRs.AbsolutePage = Cint(iAbsPage)
if RetRs.AbsolutePage > 0 then
SetAbsPage = true
else
SetAbsPage = false
end if
end function
'生成翻页的功能条
function PageBar(PageNum,Url)
Dim PageUrl,NowNum,FirstPage,PrewPage,NextPage,LastPage,PageSelect
NowNum = GetPage()
if Trim(Url) = "" then
PageUrl = Request.ServerVariables("SCRIPT_NAME") & "?"
else
if InStr(Url,"?") > 0 then
if Right(Trim(Url),1) = "&" then
PageUrl = Trim(Url)
else
PageUrl = Trim(Url) & "&"
end if
else
PageUrl = Trim(Url) & "?"
end if
end if
if NowNum <= 1 then
FirstPage=""
PrewPage = ""
else
FirstPage = "href='#' onClick=javascript:JumpPage('"& PageUrl &"Page=1');"
PrewPage = "href='#' onClick=javascript:JumpPage('"& PageUrl &"Page="& NowNum-1 &"');"
end if
if cint(NowNum) >= cint(PageNum) then
NextPage = ""
LastPage = ""
else
NextPage = "href='#' onClick=javascript:JumpPage('"& PageUrl &"Page="& NowNum+1 &"');"
LastPage = "href='#' onClick=javascript:JumpPage('"& PageUrl &"Page="& PageNum &"');"
end if
PageSelect = ""
for i = 1 to PageNum
if i = NowNum then
PageSelect = PageSelect & "<option value='" & PageUrl & "Page=" & i & "' selected>" & i & "</option>/n"
else
PageSelect = PageSelect & "<option value='" & PageUrl & "Page=" & i & "'>" & i & "</option>/n"
end if
next
PageBar = ""
PageBar = PageBar & "<table border='0' width='98%' align='center'><tr><td align='right'>"
PageBar = PageBar & "<label class='body'>Total <span class='bodyred'>" & PageNum & "</span> pages,"
PageBar = PageBar & "Now is <span class='bodyred'>" & NowNum & "</span> "
PageBar = PageBar & "<span class='bodylink'>"
PageBar = PageBar & "[<a class='bodylink'" & FirstPage & ">First</a>/"
PageBar = PageBar & "<a class='bodylink'" & PrewPage & ">Prew</a>]"
PageBar = PageBar & "[<a class='bodylink'" & NextPage & ">Next</a>/"
PageBar = PageBar & "<a class='bodylink'" & LastPage & ">Last</a>]"
PageBar = PageBar & "</span>Jump to "
PageBar = PageBar & "<select name='SelJumpTo' onChange='javascript:JumpPage(this.value);'>" & PageSelect & "</select>"
PageBar = PageBar & "</label></td></tr></table>"
end function %>