Click here to Skip to main content
15,891,868 members
Articles / Web Development / ASP.NET

Pagination using XSL

Rate me:
Please Sign up or sign in to vote.
1.09/5 (17 votes)
15 Apr 2010CPOL 64.8K   12   11
To display data in multiple pages using XSL.

Introduction

Below is a way to display your xml in multiple pages with Prev and Next links. You could customize it to display any number of entries on a page. It takes in 2 parameters that could be passed as querystrings to the page.

Page - should hold the value of which page is currently displayed.
PageSize - should hold the value of how many entries should be displayed in a page.
_dirresult - replace this with the url to the page that does the transformation

Note: First page id is 0

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<a href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</a>">
<xsl:output method="html" version="3.2" encoding="ISO-8859-1"/>
<xsl:param name="Page" select="0" />
<xsl:param name="PageSize" select="1" />
 
<xsl:template name="results" match="/">


<xsl:variable name="mycount" select="count(root/customer)"/>
<xsl:variable name="selectedRowCount" select="floor((number($numberOfRecords)-1) div $recordsPerPage)+1"/>


      <xsl:for-each select="root/customer">
       <!-- Pagination logic -->
       <xsl:if test="position() &gt;= ($Page * $PageSize) + 1">
        <xsl:if test="position() &lt;= $PageSize + ($PageSize * $Page)">


         <!-- Do display here -->
         
        </xsl:if>
       </xsl:if>
      </xsl:for-each>


      <!-- Prev link for pagination -->
      <xsl:choose>
       <xsl:when test="number($Page)-1 &gt;= 0">&#160;
        <A>
         <xsl:attribute name="href">_dirresult?page=<xsl:value-of select="number($Page)-1"/>&amp;pagesize=<xsl:value-of 
select="$PageSize"/></xsl:attribute>
          &lt;&lt;Prev
        </A>
       </xsl:when>
       <xsl:otherwise>
        <!-- display something else -->
       </xsl:otherwise>
      </xsl:choose>
      
      <xsl:if test="$selectedRowCount &gt; 1">
       &#160;<b class="blacktext"><xsl:value-of select="number($Page)+1"/>&#160;of&#160;<xsl:value-of 
select="number($selectedRowCount)"/></b>&#160;
      </xsl:if>
               
      <!-- Next link for pagination -->
      <xsl:choose>
       <xsl:when test="number($Page)+1 &lt; number($selectedRowCount)">&#160;
        <A>
         <xsl:attribute name="href">_dirresult?page=<xsl:value-of select="number($Page)+1"/>&amp;pagesize=<xsl:value-of 
select="$PageSize"/></xsl:attribute>
          Next&gt;&gt;
        </A>
       </xsl:when>
       <xsl:otherwise>
        <!-- display something else -->
       </xsl:otherwise>
      </xsl:choose>

   
 </xsl:template>
</xsl:stylesheet>

<pre>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRE: Pagination using XSL Pin
q25821-Nov-08 0:16
q25821-Nov-08 0:16 
QuestionNot understood properly Pin
rascalpranesh23-Jun-08 22:53
rascalpranesh23-Jun-08 22:53 
QuestionCannot go next page, the parameter Page and PageSize not changing on next page Pin
PaillaRajashekarReddy13-May-08 1:18
PaillaRajashekarReddy13-May-08 1:18 
AnswerRe: Cannot go next page, the parameter Page and PageSize not changing on next page Pin
salahudeenDiamond10-Mar-09 5:14
salahudeenDiamond10-Mar-09 5:14 
GeneralCannot go next page, the parameter Page and PageSize not changing on next page Pin
EhteshamJax15-Aug-07 1:15
EhteshamJax15-Aug-07 1:15 
Questionhow to use pagination to list the next records using next link Pin
chennaivijayan31-May-06 22:11
chennaivijayan31-May-06 22:11 
AnswerRe: how to use pagination to list the next records using next link Pin
EhteshamJax15-Aug-07 1:17
EhteshamJax15-Aug-07 1:17 
GeneralRe: how to use pagination to list the next records using next link Pin
salahudeenDiamond10-Mar-09 5:15
salahudeenDiamond10-Mar-09 5:15 
GeneralRe: how to use pagination to list the next records using next link Pin
highpowered22-Oct-10 16:42
highpowered22-Oct-10 16:42 
QuestionQuick questions. Pin
eddiephoenix2-Feb-06 4:33
eddiephoenix2-Feb-06 4:33 
GeneralDuh! Pin
Don Miguel10-Aug-05 23:40
Don Miguel10-Aug-05 23:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.