我使用此代码在经典的asp + Mysql中显示记录集的分页:
<ul class="pagination">
<% IF Cint(PageNo) > 1 then %>
<li><a rel="1" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="1">Prime</a></li>
<li><a rel="<%=PageNo-1%>" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="<%=PageNo-1%>"><</a></li>
<% End IF%>
<% For intID=1 To TotalPage%>
<% if intID=Cint(PageNo) Then%>
<li><a href="" class="selected"><%=intID%></a></li>
<%Else%>
<li><a rel="<%=intID%>" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="<%=intID%>"><%=intID%></a></li>
<%End IF%>
<%Next%>
<% IF Cint(PageNo) < TotalPage Then %>
<li><a rel="<%=PageNo+1%>" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="<%=PageNo+1%>">></a></li>
<li><a rel="<%=TotalPage%>" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="<%=TotalPage%>">Ultime</a></li>
<% End IF%>
</ul>
但是,如果我有很多页面结果,它会显示一长串的数字....如何在更改页面时只显示5页,显示下一个?
像这样:首先&lt; 1 2 3 4 5>最后
如果我点击5
首先&lt; 5 6 7 8 9>最后
等...
答案 0 :(得分:2)
我在ASP中需要一个类似的解决方案。 我找不到任何有用的东西,所以我自己做了一些东西。
我通常只用PHP编程,但在我做这个的情况下这是不可能的。 因此,如果我的代码有点slopy,请随时改进;)
我做的一个小功能:
<%
page = cInt(Request.QueryString("pg"))
pages = cInt(number of pages)
if page <1 then page = 1
if page > pages then page = pages
Function paginationHTML(page, lastPage, URL)
if page >1 then
paginationHTML = "<a href=""" & URL & page-1 & """>prev</a>"
paginationHTML = paginationHTML & "<a href=""" & URL & "1"">1</a>"
end if
if page >2 then
paginationHTML = paginationHTML & "<a>...</a>"
end if
if page >0 then
paginationHTML = paginationHTML & "<a href=""" & URL & page & """ class='jp-current'>"& page &"</a>"
end if
if lastPage >2 then
if page < lastPage-1 then
paginationHTML = paginationHTML & "<a>...</a>"
end if
end if
if page < lastPage then
paginationHTML = paginationHTML & "<a href=""" & URL & lastPage & """>"&lastPage&"</a>"
paginationHTML = paginationHTML & "<a href=""" & URL & page+1 & """>next</a>"
end if
End Function
%>
像这样使用:
<%=paginationHTML(page,pages,"?pg=")%>
答案 1 :(得分:1)
如果您想保留其他一些查询字符串参数,此代码也可以使用。它删除页面值,添加新页面值并构建分页控件html。
它使用Twitter Bootstrap样式:http://twitter.github.io/bootstrap/components.html#pagination
用法:
Response.Write PagingControl(10, 30, "?field-keywords=whatever&page=7")
代码:
Public Function RemoveEmptyQueryStringParameters(strQueryString)
If IsNullOrEmpty(strQueryString) Then Exit Function
Dim strNewQueryString: strNewQueryString = ""
strQueryString = Replace(strQueryString, "&", "&")
strQueryString = Replace(strQueryString, "?", "&")
Dim arrQueryString: arrQueryString = Split(strQueryString ,"&")
For i=0 To UBound(arrQueryString)
strTempParameter = Left( arrQueryString(i), Instr( arrQueryString(i) & "=", "=" ) - 1 )
strTempParameterValue = Right( arrQueryString(i), Len( arrQueryString(i) ) - InstrRev( arrQueryString(i), "=" ) )
If Not IsNullOrEmpty(strTempParameterValue) Then
strNewQueryString = strNewQueryString & "&" & arrQueryString(i)
End If
Next
If InStr(strNewQueryString,"&") = 1 Then
strNewQueryString = "?" & Right(strNewQueryString, Len(strNewQueryString) - 1)
End If
strNewQueryString = Replace(strNewQueryString, "&", "&")
Erase arrQueryString
Set arrQueryString = Nothing
RemoveEmptyQueryStringParameters = Trim(strNewQueryString)
End Function
Public Function AddQueryStringParameter(ByVal strQueryString, ByVal strParameter, ByVal strValue)
Dim strNewQueryString: strNewQueryString = ""
strNewQueryString = Replace(strQueryString, "&", "&")
strNewQueryString = Replace(strNewQueryString, "?", "&")
strNewQueryString = strNewQueryString & "&" & strParameter & "=" & strValue
If InStr(strNewQueryString,"&") = 1 Then
strNewQueryString = "?" & Right(strNewQueryString, Len(strNewQueryString) - 1)
End If
strNewQueryString = Replace(strNewQueryString, "&", "&")
AddQueryStringParameter = Trim(strNewQueryString)
End Function
Public Function PagingControl(ByVal intPage, ByVal intPageCount, ByVal strQueryString)
If intPageCount <= 1 Then
PagingControl = ""
Exit Function
End If
strQueryString = RemoveEmptyQueryStringParameters(strQueryString)
strQueryString = RemoveQueryStringParameter(strQueryString, "page")
Dim strQueryStringPaging: strQueryStringPaging = ""
Dim strHtml: strHtml = "<div class=""pagination""><ul>"
If cInt(intPage) > 1 Then
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", "1")
strHtml = strHtml & "<li><a href=""" & strWebSiteUrl & strQueryStringPaging & """>Anfang</a></li>"
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", CInt(intPage - 1))
strHtml = strHtml & "<li><a href=""" & strWebSiteUrl & strQueryStringPaging & """>< Zurück</a></li>"
Else
strHtml = strHtml & "<li class=""disabled""><a href=""#"">Anfang</a></li>" & _
"<li class=""disabled""><a href=""#"">< Zurück</a></li>"
End If
Dim intPagesToShow: intPagesToShow = 10
If intPageCount >= intPagesToShow Then
If Cint(intPage)>Int(intPagesToShow/2) Then
If Cint(intPage)>(intPageCount-Int(intPagesToShow/2)) Then
intStart = intPageCount-intPagesToShow
intEnd = intPageCount
Else
intStart = intPage-Int(intPagesToShow/2)
intEnd = intPage+Int(intPagesToShow/2)
End If
Else
intStart = 1
intEnd = intPagesToShow
End If
Else
intStart=1
intEnd=intPageCount
End If
If intStart=0 Then
intStart=1
End If
For i = intStart To intEnd
If Cint(intPage)=i Then
strHtml = strHtml & "<li class=""active""><a href=""" & strWebSiteUrl & strQueryStringPaging & """>" & i & "</a></li>"
Else
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", Cint(i))
strHtml = strHtml & "<li><a href=""" & strWebSiteUrl & strQueryStringPaging & """>" & i & "</a></li>"
End If
Next
If cInt(intPage) < cInt(intPageCount) Then
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", CInt(intPage + 1))
strHtml = strHtml & "<li><a href=""" & strWebSiteUrl & strQueryStringPaging & """>Vorwärts ></a></li>"
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", Cint(intPageCount))
strHtml = strHtml & "<li><a href=""" & strWebSiteUrl & strQueryStringPaging & """>Ende</a></li>"
Else
strHtml = strHtml & "<li class=""disabled""><a href=""#"">Vorwärts ></a></li>" & _
"<li class=""disabled end""><a href=""#"">Ende</a></li>"
End If
strHtml = strHtml & "</ul></div>"
PagingControl = Trim(strHtml)
End Function