![]() |
![]() |
|
|
Viewing records one page at a time with ASP
by Greg Covey 25 February, 2001 You’ve got a ton of data to display to your users, but you don’t want to overwhelm them. What’s the best thing to do? Divide the data up into smaller sets, or ‘pages,’ and give them links to navigate to previous or next pages. We’ve all seen this. Every search engine displays its information in this manner. The resulting HTML pages are much smaller, load faster, and keep your viewers attention. Page properties of the Recordset ObjectBy utilizing the PageSize and AbsolutePage properties of the recordset object, we can build such an interface with ASP quite easily. I’ve built this example with two files. ‘Pages.asp’ actually builds and displays the table, and ‘pages_support.asp’ contains the functions needed to complete this task. Let’s first look at ‘pages.asp.’ IntPageSize = 5 First, we set an integer value to the variable intPageSize. A few lines down we assign this value to the PageSize property of the recordset. This will be the number of records displayed per each page. Lines two and three check for a query string parameter passed to the page. This value is used to indicate what page to display. If the value is empty, then we set it to 1 to display the first page in the recordset. We then assign this value to the AbsolutePage property of the recordset object. TotalPages = rst.PageCount The next section of code assigns values to variables that are necessary to build our navigation system. TotalPages is the total number of pages in the recordset, based on the previously assigned PageSize property. The variables intPrev and intNext define the previous and next page in the recordset, relative to the current page, PageIndex. do while NOT rst.EOF AND Count <= IntPageSize The next chunk of code builds the table header and rows, and should need no explanation except for the do while statement. By restricting our condition in the do while statement, we’re only stepping through the defined number of items on any one page. Build the navigation systemThe method that I employ to build the navigation system is the bulk of the other file, pages_support.asp. I’ve put this code in a separate function for obvious reasons. I call this function with the following line in pages.asp: response.write BuildNav (intPrev,IntNext,TotalPages) As you can see, it needs the number of the previous page, the next page, and all pages in the recordset. Looking at the funciton BuildNav in pages_support.asp, we first check the status of the passed variable, intPrev. if intPrev <> 0 then If it equals zero, we know that were on the first page of the recordset, so we don’t add the ‘previous’ link. I’ve concatenated some non-breaking spaces as well, for the sake of formatting. The same thing is done a bit lower for the ‘next’ link. if (rst.AbsolutePage <> -3) then The only difference here is that were checking the value of the AbsolutePage property: we know that if it equals –3, the current page is the last page in the recordset. The rest of the code in this function builds the page numbers in between the previous and next links. if cint(counter) = cint(PageIndex) then As it steps through the loop of total pages, it checks if the current page in the recordset matches the count. If it does, that page number is NOT displayed as a link. The other two functions are used to make the connection to the database, and method of writing some non-breaking spaces to any text stream. pages.asp © 1999-2002 |