I have an Apex command button in my Visual Force page. Using that button I want to call another visual force page, which in turn should call action function written in the controller.
Please advise.
I have an Apex command button in my Visual Force page. Using that button I want to call another visual force page, which in turn should call action function written in the controller.
Please advise.
Put the below code in the controller action method that is being called on click of command button
PageReference pr = new PageReference('/apex/yourVFPageName');
pr.getParameters().put('key','value');
pr.setRedirect(true); // If you want a redirect. Do not set anything if you want a forward.
return pr;
Edit:
In the constructor of the 'yourVFPageName' page controller you can call any method.
As @Vigneshwaran G has mentioned:
use action attribute in page to call that method. Otherwise if the method being called from constructor contains any DML statements it will throw error.
visual force page code:
<apex:commandButton value="button" action="{!redirect}"/>
in the controller define this function:
public PageReference redirect()
{
PageReference pr = new PageReference('/apex/yourVFPageName');
return pr;
}
visual force code:
<apex:commandButton value="redirect" action="{!redirect}"/>
controller:
public PageReference redirect() {
return Page.existingPageName;
}
Advantage of declaring this way is:
PageReference for a Visualforce page that has already been saved in your organization. By referring to a page in this way, the platform recognizes that this controller or controller extension is dependent on the existence of the specified page and will prevent the page from being deleted while the controller or extension exists.