0

I am trying to, upon clicking either Back or Next, increment or decrement a variable (tranquerypage), and then reload the current page with the variable's new value which can then be queried upon the loading of the page. However, I tried a few methods, and being new to ASP classic I had issues getting it to work properly. The vbscript methods have not been working. Here is what I have so far -

    <%
tranquerypage = session("tranquerypage")
if session("tranquerypage") = "" then 
session("tranquerypage") = 1 
end if
%>

<INPUT TYPE=button Name=temp Value="Back" target="_self" onClick="<%call back(tranquerypage)%>">&nbsp;&nbsp;<INPUT TYPE=button Name=temp Value="Next" target="_self" onClick="<%call nextpage(tranquerypage)%>">
</td>
</tr>

    <%
sub back(tranquerypage)
tranquerypage = tranquerypage - 1 
end sub

sub nextpage(tranquerypage)
tranquerypage = tranquerypage + 1
end sub
%>
2
  • If you put NextPage with +1, may be BackPage -1 or you always increment +1 page
    – Mastercafe
    Apr 8, 2015 at 16:18
  • 1
    I prefer use Cookie (response.cookies and request.cookies) to pagination vars. You can use more than one value, the user don't lost his position during navigation if maintain many time the page
    – Mastercafe
    Apr 8, 2015 at 16:21

1 Answer 1

3

The HTML/JavaScript code runs in the browser and the ASP code runs on the server, quite possibly on two completely different computers.

Also, the browser runs JavaScript and that is entirely incompatible with ASP.

That means one thing: You can't call any ASP functions from HTML. End of story.


The interface to communicate with a web server is: HTTP messages. In other words, only by requesting URLs you can inform the server of actions on the client.

So the first thing you need to do is determine how you want to communicate your intent to the server, i.e. how the URL should look like.

How about:

http://yourserver.com/yourpage.asp?page=next
http://yourserver.com/yourpage.asp?page=prev
http://yourserver.com/yourpage.asp?page=5

Now that we decided on the URL, make the ASP page understand it. URL parameters are available in the Request collection.

<% 
Option Explicit ' never work without Option Explicit

Dim page, requestedPage, MAX_PAGE

requestedPage = LCase(Request("page"))
MAX_PAGE = 10

If requestedPage = "next" Then
   page = NextPage()
ElseIf requestedPage = "prev" Then
   page = PrevPage()
Else
   page = SetPage(requestedPage)
End
%>

Now your ASP code has an idea what to do with the "page" URL parameter.

Let's define the functions that do the page actual switching.

<% 
Function CurrentPage
    If IsNumeric(Session("page")) Then
        CurrentPage = Session("page")
    Else
        CurrentPage = 1
    End If 
End Function

Function NextPage
    NextPage = SetPage(CurrentPage() + 1)
End Function

Function PrevPage
    PrevPage = SetPage(CurrentPage() - 1)
End Function

Function SetPage(newPage)
    ' range check!
    If Not IsNumeric(newPage) Then
        Session("page") = 1
    ElseIf page < 1 Then
        Session("page") = 1
    ElseIf page > MAX_PAGE Then
        Session("page") = MAX_PAGE
    Else
        Session("page") = Int(newPage)
    EndIf
    SetPage = Session("page")
End Function
%>  

The easiest way to send an HTTP message to the server ("to request an URL") is with a link. So your HTML could look like this:

<a href="/yourpage.asp?page=prev">Previous Page</a>
<a href="/yourpage.asp?page=5">Page 5</a>
<a href="/yourpage.asp?page=next">Next Page</a>

Your remaining tasks:

  • Output an actual page of content, of course.
  • Figure out how to output more links than just Page 5.
  • Figure out how to set MAX_PAGE to something more appropriate than 10.
  • Figure out how to use different page sizes.
  • And finally: Think about dropping the entire "current"/"next"/"prev" page concept. Every page of data is uniquely addressable with its URL. If you are on http://yourserver.com/yourpage.asp?page=5, it follows naturally what the previous and the next pages are - 4 and 6. Figure out a way to do page switching entirely via URL parameters and without any session or cookie value.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.