apex:commandButton Component
The apex:commandButton
component is a Visualforce component in Salesforce that creates a button on a page to run an Apex action method. It is used to execute a specific action defined in an Apex controller when the user clicks on the button.
Syntax
The syntax for apex:commandButton
in Visualforce page is as follows:
<apex:commandButton value="Button Label" action="{!controllerMethod}" />
value
: Specifies the label of the button.action
: Specifies the name of the controller method to invoke when the button is clicked.
Example
Consider the following example:
<apex:page controller="MyController">
<apex:form >
<apex:commandButton value="Click Me" action="{!doAction}" />
</apex:form>
</apex:page>
In the above example, when the user clicks on the "Click Me" button, the doAction
method in the MyController
controller is executed.
Explanation
The apex:commandButton
component is used to create a button on a Visualforce page that executes a specific action defined in the associated Apex controller. When the user clicks on the button, the specified action is invoked.
Use
The apex:commandButton
component is used to create a button that performs a specific action when clicked. It can be used for various scenarios such as submitting a form, saving data, or navigating to another page.
Important Points
- The
apex:commandButton
component must be enclosed within anapex:form
component to work properly. - The
action
attribute must refer to a valid method in the controller or extension class. - The
rerender
attribute can be used to refresh specific portions of the page after theapex:commandButton
action has been executed.
Summary
The apex:commandButton
component in Salesforce is used to create a button on a Visualforce page that executes a specific action defined in the associated Apex controller. It is commonly used to submit a form or save data in a Salesforce application. The apex:commandButton
must be enclosed within an apex:form
component and the action
attribute must refer to a valid method in the controller or extension class. The rerender
attribute can be used to refresh specific portions of the page after the apex:commandButton
action has been executed.