1

I have two visualforce pages and one class.

At the first page (calc) I have just Information about some Company Details and a command button which connect to a second page (calc_test). At the second page you can calculate a bit.

I wanna set the variable calc_view to 1 if I click at the button. Below you will find my code, but it doesn't work.

I think the value get lost if I click the button, but why? I tried also with action argument and an additional method of:

public PageReference calc_click() {
    calc_view = 1;
    return new PageReference('/apex/calc_test');
} 

but it doesnt work.

Visualforce Page: calc

<apex:commandButton value="CALC" onclick="window.open('/apex/calc_test','_blank','height=500,width=500,toolbar=no,scrollbars=no,resizeable=no,menubar=no,top=200,left=200'); return false;">
    <apex:param name="TEST" value="1" assignTo="{!calc_view}"/>
</apex:commandButton>

Visualforce Page: calc_test

<apex:page controller="calc_class" showheader="false" sidebar="false">
<apex:form >
<apex:outputPanel rendered="{!If(calc_VIEW=1,true,false)}">   
<apex:inputText value="{!value1}" maxlength="10" style="width:40px"/>
    <br/>
<apex:outputText value="{0, number, #,##0}"><apex:param value="{!value2}"/></apex:outputText>
    <br/>
<apex:commandbutton value="calc" action="{!Pr_calc}" />

</apex:outputPanel>      
</apex:form>
</apex:page>

Class: calc_class

public class calc_class {

    public Integer calc_VIEW {get; set;}

    public Double value1 {get; set;}
    public Double value2 {get; set;}

    public calc_class() {
        calc_VIEW = 0;
    }

    public PageReference Pr_calc() {
        value2= value1/ 3.35 * 100;
        return new PageReference('/apex/calc_test');   
    }

}
  • Did you try passing the value as a query parameter to second page? – Raul Mar 3 '17 at 14:30
2

If you want to preserve the data across the controllers, you have to use setRedirect(redirect).

As per documentation:

If setRedirect(false), the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

Try changing the code to:

public PageReference calc_click() {
    calc_view = 1;
    PageReference pr = new PageReference('/apex/calc_test');
    pr.setRedirect(false);
    return pr;
} 

Other option would be to use send data to next page as a query parameter.

| improve this answer | |

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.