+ Start a Discussion
sfdeveloper9sfdeveloper9 

How to show section on VF page on button click

I have a requirement to show a section only on button click. here is the code I have tried, but no luck. can someone tell me what is the error in the below code

 

VF code :

 

<apex:page controller="testRenderCtrl">
<apex:form>
    <apex:commandButton value="Submit" action="{!submit}" reRender="panelId"/>
    <apex:outputPanel  rendered = "{!flag}"  id = "panelId">
    <apex:pageblock >
        <apex:pageblockSection title = "Contact Information">
            <apex:inputField value="{!con.firstname}"/>
            <apex:inputField value="{!con.lastname}"/>                                              
        </apex:pageblockSection>
    </apex:pageblock>
    </apex:outputPanel>
    </apex:form>
</apex:page>

 

 

Controller code

 

public class testRenderCtrl {
    public Contact con {get;set;}    
    public boolean flag = false;    
    public boolean getFlag(){
        return flag;
    }
    public void setFlag(boolean flg){
        flag = flg;
    }    
    public void submit(){
     System.debug('****');
     flag = true;   
    }
}

 

Thanks,

YP

 

dwwrightdwwright

 

public PageReference submit(){
     System.debug('****');
     flag = true; 
     return null;  
    }

 Change your submit() method to the above, and it should work. action="" parameters require a method that takes no parameters and returns a pagereference. You don't even need the rerender on the commandButton.

 

Pradeep_NavatarPradeep_Navatar

Try this using javascript on the button click. Sample code is given below :

 

      <apex:commandbutton onclick = "showsection()">

      function showsection()

      {

      document.getElementById('{!$Component.pageid:formid:pageblocksectionid}').style.display = 'block';

       }

 

Hope this helps.