+ Start a Discussion
App_Dev1App_Dev1 

Passing data between controllers

Say I have page A which uses controller X, and page B controller Y. I want some continuity between the pages so that some variables in X are passed to Y when the user clicks a link in page A to visit page B.

I've implemented this with commandLink. The action method in X which transitions to page B includes a bunch of code that copies the relevant state variables into the request parameters. Example:

public PageReference goPageB() {

  PageReference ref = Page.pageB;

  ref.getParameters().put('p1', p1);

  ref.getParameters().put('p2', p2);

  ref.setRedirect(true);

  return ref;

}

This works fine. But I'm wondering if there's a better way. I'd rather pass the variables in a POST, or in a shared session object that both pages reference.


dchasmandchasman
WIth visualforce's concept of viewstate you can have multiple pages bound to the same controller and any non-transient data members in your controller /or extensions will be restored server side as part of each action method request (full page or partial page (ajax) requests are both supported). Just continue to have your action method return a PageReference but make pageA and pageB use the same value for their <apex:page controller> attribute.

We are also looking at providing some form of object-based state passing in the Summer '08 timeframe that would allow you to marshall complex object refs from one controller to another but I am just starting the design work for that...
Marty Y. ChangMarty Y. Chang

I've found, at least with the Summer '10 release, that not only does the controller have to be the same, the extensions must also be the same in order to pass data from page to page.

 

"Passing Data between Visualforce Pages with Controllers and Extensions"

http://frombelvideres4thfloor.blogspot.com/2010/08/passing-data-between-visualforce-pages.html

vipulpahwavipulpahwa

I am facing a similar issue...Here is the code

 

PAGE - FirstPage
<apex:page standardController="Design__c" extensions="DesignController" action="{!init}">
<!-- SOME TEXT DISPLAYED -->
  <apex:outputpanel>
    <apex:commandButton action="{!openSecondPage}" value="Open Second Page"/>
  </apex:outputpanel>
</apex:page>

 

CLASS
global class DesignController {
  public DesignController(ApexPages.StandardController controller){
    parentId = controller.getRecord().Id;
  }

  public PageReference init(){
    application = new MetadataWrapper.Application();
    ....
    //PERFORM SOME ACTION TO POPULATE application object
    ...
    return null;
  }

  public PageReference openSecondPage(){
    return Page.SecondPage;
  }

 

  public Class Wrapper {
    public String name {get; set;}
    public String typr {get; set;}
    public Boolean isSelected {get; set;}

    public Wrapper(String name, String type, Boolean isSelected){

      this.name = name;
      this.type = type;
      this.isSelected = isSelected;
    }
  }
}

 

In this case if I dont set setRedirect to true, on the click of the button, the page (FirstPage) reloads again thereby invoking the action function (init) and the second page (SecondPage) does not load.
However, if i do set setRedirect to true, the SecondPage loads but the data (application object) is not passed through.

Any thoughts on how this behaviour can be rectified?