1

我使用此代码在经典 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 页,而当更改页面时,显示下一个?

像这样:

第一个 < 1 2 3 4 5 > 最后一个

如果我点击 5

第一个 < 5 6 7 8 9 > 最后一个

ETC...

4

2 回答 2

2

如果您想保留一些其他查询字符串参数,此代码也可以工作。它删除页面值,添加新页面值并构建分页控件 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, "&amp;", "&")
        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, "&", "&amp;")

        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, "&amp;", "&")
        strNewQueryString = Replace(strNewQueryString, "?", "&")

        strNewQueryString = strNewQueryString & "&" & strParameter & "=" & strValue

        If InStr(strNewQueryString,"&") = 1 Then
            strNewQueryString = "?" & Right(strNewQueryString, Len(strNewQueryString) - 1)
        End If
        strNewQueryString = Replace(strNewQueryString, "&", "&amp;")

        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 & """>&lt; Zur&uuml;ck</a></li>"
      Else
        strHtml = strHtml & "<li class=""disabled""><a href=""#"">Anfang</a></li>" & _
        "<li class=""disabled""><a href=""#"">&lt; Zur&uuml;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&auml;rts &gt;</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&auml;rts &gt;</a></li>" & _
         "<li class=""disabled end""><a href=""#"">Ende</a></li>"
      End If

      strHtml = strHtml & "</ul></div>"

      PagingControl = Trim(strHtml)
    End Function
于 2013-06-04T05:33:52.867 回答
1

我需要 ASP 中的类似解决方案。我找不到任何有用的东西,所以我自己做了一些东西。

我通常只用 PHP 编程,但在我做这个的情况下这是不可能的。因此,如果我的代码有点草率,请随时改进;)

我做了一个小功能:

<%

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=")%>
于 2013-09-25T12:26:23.553 回答