Activity › Forums › Salesforce® Discussions › Can we used multiple controllers on a single visualforce page ?
-
Can we used multiple controllers on a single visualforce page ?
Posted by Hariom Chaudhary on September 4, 2019 at 12:04 PMCan we used multiple controllers on a single visualforce page ?
Achintya replied 6 years, 8 months ago 2 Members · 1 Reply -
1 Reply
-
You can’t directly reference the other classes (extensions) in the same page, but you can make variables static, assuming they participate in the form; this requirement exists because static variables are transient (do not serialize into the view state). A simple example follows:
Page:
<apex:page standardController="Account" extensions="ext1,ext2"> <apex:form > <apex:inputText value="{!message}"/> <apex:outputText value="{!ext1message}"/> </apex:form> </apex:page>Ext1:
public with sharing class ext1 { public static string message { get; set; } public ext1(ApexPages.StandardController controller) { } }Ext2:
public with sharing class ext2 { public ext2(ApexPages.StandardController controller) { } public string getext1message() { return ext1.message; } }When you type a message and press Enter, it will be echoed back from ext2’s getExt1Message() function. As you can see, you can use static variables. The caveat is that message is bound to a form element and will be submitted each time the page posts. Without the apex:inputText element, this code wouldn’t work.
Since your comments specify that it is a boolean value that is calculated, you should probably store the results of the value into an apex:inputHidden element so that the form will correctly pass the value between extensions.
Log In to reply.