Friday, September 12, 2014

Pass value from visualforce page to controller

Pass value from visualforce page to controller

The first thing a apex programmer wants to know is: how do we communicate between visual force page and  controller. How to pass parameters from a visualforce page to a controller class ?

Lets develop a example that will capture a input in the visualforce page and pass the input value to the controller.


Visualforce Page:

<apex:page controller="passparamController">
    <!-- Pass parameters from visualforce page to controller -->
    <apex:form >
            <apex:pageblock >
                  Input Here <apex:inputText value="{!myinput}"/>
                 <apex:commandButton value="Submit" reRender="outputID" action="{!MyMethode}"/>
            </apex:pageblock>
            <apex:pageblock >
                 <b>Output here = </b><apex:outputText value="{!myoutput}" id="outputID">
                 </apex:outputText>
            </apex:pageblock>
    </apex:form>
</apex:page>


Controller

Public with sharing class passparamController {
  Public string myInput{get;set;}
  Public string myoutput{get;set;}
   
  Public void MyMethode(){
   myoutput = myInput ;
  }
}




In this example,
your {get;set} variable is binded to your input text box which makes it possible to get the value in the controller and also vice versa.

Thus when you press button the input value is passed on to controller.
<apex:inputText value="{!myinput}"/> your input text box is bind to the variable myinput string which is defined as get;set;

When the method is called from the submit button this value from myinput string is assigned to the myoutput string. This is proved when your input value is displayed in output section.

Note: Whenever we have any component wherein we want to input a value it is necessary that that component is enclosed between <apex:form>






1 comments:

Oracle Equipments said...

Nice post...I look forward to reading more, and getting a more active part in the talks here, whilst picking up some knowledge as well..

Pass Box manufacturers

Post a Comment

 
| ,