function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Deekshant SharmaDeekshant Sharma 

Maintain View State after reload

Hi, I am trying to maintain the viewstate of my Page after reload. My VF page uses pagination to reduce the data held by the viewstate. I want to stay on the same page number after my page reloads but it wouldn't work and refreshes the page back to page number 1.
Here's my Controller and Page.
  • Controller:
public class CustomPagination2 {
	private Integer currentPage;
    private Integer recPerPage, offsetValue, totalPages, totalRecords;
    private String query, sortHow;
    List<Contact> tempLst;
    public String sortByWhat {get; set;}
    public List<Integer> itrInt {get; set;}
    public List<Contact> retList {
        get{
            return Database.query(query) ;
        }
        set;}
    public Integer pageFromVf{get; set;}
    
    public CustomPagination2(){
        sortByWhat = 'FirstName';
        sortHow = 'asc';
        query = 'SELECT FirstName, LastName FROM Contact order by '+sortByWhat+' '+sortHow+' LIMIT :recPerPage OFFSET :offsetValue';
        if(currentPage == null) currentPage = 1;
        recPerPage = 7;
        offsetValue = (currentPage - 1) * recPerPage;
        totalPages = 0;
        totalRecords = 0;
        itrInt = new List<Integer>();
        for(Contact con : [SELECT FirstName, LastName FROM Contact]){
            if(math.mod(totalRecords, recPerPage) == 0){
                itrInt.add(totalPages+1);
                totalPages++;
            }
            totalRecords++;
        }
    }
    
    public void changeOrder(){
        tempLst = retList;
        if(sortHow=='asc') sortHow='desc';
        else sortHow = 'asc';
        query = 'SELECT FirstName, LastName FROM Contact WHERE id IN :tempLst order by '+sortByWhat+' '+sortHow;
    }
    
    public void changeOrder2(){
        if(sortHow=='asc') sortHow='desc';
        else sortHow = 'asc';
        query = 'SELECT FirstName, LastName FROM Contact order by '+sortByWhat+' '+sortHow+' LIMIT :recPerPage OFFSET :offsetValue'; 
    }
    
    public void next(){
        if(currentPage!=totalPages){
            currentPage++;
            setQuery();
        }
        else{
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR, 'No page after this one!'));
        }
    }
    
    public void last(){
        currentPage = totalPages;
        setQuery();
    }
    
    public void previous(){
        if(currentPage!=1){
        	currentPage--;
            setQuery();
        }
        else{
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,'No page before this!'));
        }
    }
    
    public void first(){
        currentPage = 1;
        
        setQuery();
    }
    
    public Integer getPageno(){
        return currentPage;
    }
    
    public String getRecords(){
        String str = 'Showing records '+(offsetValue+1)+'-';
        if(offsetValue+recPerPage <= totalRecords) str+= (offsetValue+recPerPage)+' out of '+totalRecords;
        else str += totalRecords+' out of '+totalRecords;
        return str;
    }
    
    public void goByPageNo(){
        if(pageFromVf>0 && pageFromVf<=totalPages){
            currentPage = pageFromVf;
            setQuery();
        }
        else{
            ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,'This page does not exist!'));
        }
    }
    
    public void setQuery(){
        sortHow = 'asc';
        sortByWhat = 'FirstName';
        offsetValue = (currentPage - 1) * recPerPage;
        query = 'SELECT FirstName, LastName FROM Contact order by '+sortByWhat+' '+sortHow+' LIMIT :recPerPage OFFSET :offsetValue';
    }
}
  • VF Page
<apex:page controller="CustomPagination2">
	<apex:pageBlock >
        <apex:pageMessages id="msg"></apex:pageMessages>
        <apex:outputPanel id="pageNo">
            <div style="font-size:20px; font-weight:bold;">{!pageno}</div> 
            <apex:outputPanel id="records"><p style="text-align:center">{!records}</p></apex:outputPanel>
        </apex:outputPanel><br/><br/>
        <apex:pageBlockSection id="conTable">
                <tr>
                	<th><a href="javascript:void(0)" onclick="changeOrderAF('FirstName')">First Name</a></th>
                    <th><a href="javascript:void(0)" onclick="changeOrderAF('LastName')">Last Name</a></th>
                </tr>
                <apex:repeat value="{!retList}" var="conn">
                <tr>
                	<td>{!conn.FirstName}</td>
                    <td>{!conn.LastName}</td>
                </tr>
                </apex:repeat>
        </apex:pageBlockSection><br/><br/>
   
  	  <apex:outputPanel onclick="firstAF()" styleClass="btn">&lt;&lt; First</apex:outputPanel>
        <apex:outputPanel onclick="previousAF()" styleClass="btn">&lt; Previous</apex:outputPanel>
        <apex:outputPanel id="pageLinks">
            <apex:repeat value="{!itrInt}" var="page">
                <apex:outputLink disabled="{!pageno == page}" onclick="gotoAF({!page})" value="javascript:void(0)" > {!page} &nbsp; </apex:outputLink>
            </apex:repeat>
        </apex:outputPanel>
        <apex:outputPanel onclick="nextAF()" styleClass="btn">Next &gt;	</apex:outputPanel>
        <apex:outputPanel onclick="lastAF()" styleClass="btn">Last &gt;&gt;</apex:outputPanel><br/><br/>
        Go to page: <input type="text" id="gotoId"/> <apex:outputPanel onclick="gotoJS()" styleClass="btn">GO!</apex:outputPanel>
    </apex:pageBlock>
    
    <apex:form >
    	<apex:actionFunction action="{!next}" reRender="conTable, msg, pageNo, pageLinks, records" name="nextAF"/>
        <apex:actionFunction action="{!previous}" reRender="conTable, msg, pageNo, pageLinks, records" name="previousAF" />
        <apex:actionFunction action="{!last}" reRender="conTable, pageNo, pageLinks, records, msg" name="lastAF"/>
        <apex:actionFunction action="{!first}" reRender="conTable, pageNo, pageLinks, records, msg" name="firstAF"/>
        <apex:actionFunction action="{!goByPageNo}" reRender="conTable, msg, pageNo, pageLinks, records" name="gotoAF"> 
        	<apex:param value="" assignTo="{!pageFromVf}" name="firstParam"/>
        </apex:actionFunction> 
        <apex:actionFunction name="changeOrderAF" action="{!changeOrder}" reRender="conTable">
        	<apex:param assignTo="{!sortByWhat}" name="firstParam" value=""/>
        </apex:actionFunction>
        <apex:actionFunction name="changeOrderAF2" action="{!changeOrder2}" reRender="conTable">
        	<apex:param assignTo="{!sortByWhat}" name="firstParam" value=""/>
        </apex:actionFunction>
    </apex:form>
    <script>
    	function gotoJS(){
        	gotoAF(document.getElementById("gotoId").value);
        }
    </script>
</apex:page>