apex:commandButton with Return Value
Below you can see a sample code that can be used to return a value from an Apex controller to a Visualforce page when calling a controller function from an apex:commandButton
. This approach doesn’t require you to use @RemoteAction
(i.e. static methods), so you doesn’t lose the stateful nature of your page, and your controller’s method can access all the variables within that controller instance.
Visualforce Page:
Controller:
That’s it. Try it, it works. Let me know in the comments if there are any drawbacks to this approach. I’m always open to constructive critique.
P.S. The same would work for apex:actionFunction
that you can call from JavaScript, see below:
Written on May 15, 2016