如果你想保留一些其他查詢字符串參數,這段代碼也可以。它刪除頁面值,添加新的頁面值並構建分頁控制html。
它使用Twitter的引導樣式: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
@喬納森 - joosten - 如何我是否使用此代碼,以便分頁開始1 ..此刻,分頁從0開始? ..乾杯 – BigJobbies
我更改了代碼,因此它不顯示頁面0. 只有在沒有頁面的情況下它才應該給出頁面0。 希望這個小小的改進可以幫助你。 –