[SalesForce] onchange event dropdownlist – re-render pageBlockSection

How can I re-render particular pageblocksection based on the onchange event from dropdownlist?

So here is what I have done:

   <apex:pageBlockSection title="Select Record Type" columns="1"> 
   <apex:selectList id="countries" value="{!RecordType}" size="1" required="true">
    <apex:selectOptions value="{!RecordTypes}"/>
     </apex:selectList>    
    </apex:pageBlockSection>

<apex:pageBlockSection rendered="{!RecordType.Name == 'employer'}" columns="1">
   <apex:outputText>Employer section</apex:outputText>
</apex:pageBlockSection>
<apex:pageBlockSection rendered="{!RecordType.Name == 'employee'}" columns="1">
   <apex:outputText> Employee section</apex:outputText>
</apex:pageBlockSection>

My record type names are:

  • Employer
  • Employee

Best Answer

Try this:

<apex:pageBlockSection title="Select Record Type" columns="1"> 
    <apex:selectList id="countries" value="{!RecordType}" size="1" required="true">
        <apex:selectOptions value="{!RecordTypes}"/>
        <apex:actionSupport event="onchange" reRender="thePanels"/>
    </apex:selectList>    
</apex:pageBlockSection>

<apex:outputpanel layout="none" id="thePanels">
    <apex:pageBlockSection rendered="{!RecordType.Name == 'employer'}" columns="1">
        <apex:outputText>Employer section</apex:outputText>
    </apex:pageBlockSection>
    <apex:pageBlockSection rendered="{!RecordType.Name == 'employee'}" columns="1">
        <apex:outputText> Employee section</apex:outputText>
    </apex:pageBlockSection>
</apex:outputpanel>