function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SDFC FirstLevelerSDFC FirstLeveler 

I want to show result on same Visual force page on a button click

Controller Class :

public Class a {
    public integer userinput{get; set;}
        
       public void getclickMe(){
        string n;
        if (userinput> 0){
         n= 'No is Positive'; 
        }
        else if (userinput< 0){
        n= 'No is Negative';         
        }
        else{
          n='Number is zero';
        }
    }
}


VFPage:

<apex:page controller="a">
<apex:form >
<apex:pageBlock title=" Take Input">
<apex:pageBlockSection >
<apex:outputLabel value="Enter Number" />
<apex:inputText value="{!userinput}" />
</apex:pageBlockSection>
<apex:pageBlockSection >
<apex:commandButton value="Find" action="{!getclickMe}"/>


<apex:outputPanel >
<apex:outputText value="{!clickMe}">
</apex:outputText>
</apex:outputPanel>

</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>

 
Shruti SShruti S

There were a couple of mistakes in your code -

  • The variable 'n' is a private variable.
  • The variable 'n' which contains the result is not a property.
  • The variable 'n' was not being used in the Visualforce page in the output panel to display the result instead the method was being called.


If we want to display something in Visualforce page, we need to create properties ( get set variables ) and use them with the help of merge fields in the Visualforce markup. In your case, the result is being stored into a private variable and it is not a property.

Hence to display this result in the Visualforce page - 

  • Make the result variable ( String n in your case ) a public property.
  • Use merge fields to display the property in the Visualforce page.

Here is how the Apex and the Visualforce code will look like -

Apex Controller

public Class Abhishek {
    public integer userinput    { get; set; }
    public String n             { get; set; }
        
       public void getclickMe() {
        if ( userinput > 0 ) {
         n = 'No is Positive';
        }
        else if ( userinput < 0 ) {
        n = 'No is Negative';
        }
        else {
          n = 'Number is zero';
        }
    }
}
Visualforce
<apex:page controller="Abhishek">
    <apex:form >
        <apex:pageBlock title=" Take Input">
            <apex:pageBlockSection >
                <apex:outputLabel value="Enter Number" />
                <apex:inputText value="{!userinput}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection >
                <apex:commandButton value="Find" action="{!getclickMe}"/>
                <apex:outputPanel >
                    <apex:outputText value="{!n}">
                    </apex:outputText>
                </apex:outputPanel>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Feel free to ask if you have any more doubts.
SDFC FirstLevelerSDFC FirstLeveler
Thank you very much for reply. Iam new to this. Assume my controller code is 
public Class a {
    
        public integer userinput    {get; set;}
        public string n             {get; set;}
        
        public string getclickMe(){
          
          if (userinput> 0){
               n='No is Positive'; 
             }
            
            else if (userinput< 0) {
                    n='No is ee';           
                 }
            else{
           n='No is eer';  
        }   
        return n;
    }
}

Now I want to run this code with same VFPage and iam geting error "Formula Expression is required on the action attributes.". Please guide  
Shruti SShruti S
What Visualforce code did you use? Could you please use the one which I gave you?
SDFC FirstLevelerSDFC FirstLeveler
Yes I used same. But didnt work. Please help Shruti, i want method to return a value