I am achieving pagination of Account records using vf page with custom controller.But the issue is that it is the Next button is functioning even when the list size has ended. How to fix that??

<apex:page controller="paginationcls">
       <apex:pageBlock id="pg">

           <apex:pageBlockButtons location="bottom">
                   <apex:commandButton value="Previous" action="{!Previous}" rerender="table" status="status"/>               
                   <apex:commandButton value="Next" action="{!Next}" rerender="table" status="status"/>
                 <apex:actionStatus id="status">
                     <apex:facet name="start">
                         <apex:image value="/img/loading32.gif" style="height: 15px;"></apex:image>

           <apex:pageBlockTable value="{!accLst}" var="acc"  id="table">
               <apex:column value="{!acc.name}"/>

Controller Class :

public class Paginationcls{

    integer recordLimit = 10;
    integer offSetLimit = 0;
    integer index;

    public list<account> acclst{get
                                {acclst = [select Id,name from Account Limit :recordLimit OFFSET :offSetLimit ];
                                return acclst;
                                } set;

    public Paginationcls(){

    public pagereference Next(){
    index = acclst.size() - 1;
         if(acclst.size()>0 && acclst.size()<index)

           offSetLimit = offSetLimit + 10;
    return null;

    public pagereference previous(){
              offSetLimit = offSetLimit -10;

    return null;
  • create a index variable. increase index++ on next button. calculate lastindex=list.size()/10; disable button when index==lastindex – Shravan Boddula Nov 10 '16 at 6:23
public boolean StillHas{get{
    if(listsize-offSetLimit <= 10){
        StillHas = true;
        stillHas = false;
} private set;}

public integer listsize {get{
        listsize = [select Id,name from Account].size();
    return listSize;

public boolean IsBase {get{
    if(offSetLimit == 0){
        IsBase = true;
        IsBase = false;
} private set;}

Populate rendered attribute of next button with StillHas on the page.

Populate rendered attribute of Previous button with !IsBase on the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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