[SalesForce] Rendering PageBlockSections based on picklist

I have a VF page to make a record for a custom object. It has 2 <pageBlockSection>. The first should always be rendered, and it contains a picklist. Depending on the value of the picklist, the second might be rendered.

What is the best way to go about this? Can the standard controller handle this, or does it need a custom controller? Why isn't my second section rendering correctly?

The picklist:

<apex:inputField value="{!CustomObject__c.Type__c}"> 
     <apex:actionSupport event="onchange" reRender="section2"/>
</apex:inputField>

And the second section:

<apex:pageBlockSection id="section2" columns="1" rendered="{!IF(CustomObject__c.Type__c == 'Functionality Request',true,false)}">  
    <apex:inputField value="{!CustomObject__c.Priority__c }"/>

</apex:pageBlockSection>

Best Answer

A custom controller is not needed. You need to re-render the container holding the component that has the conditional rendering expression.

Here is a working example:

<apex:page standardController="Contact">
<apex:form>
<apex:pageBlock id="pb">
    <apex:pageBlockSection>  
        <apex:inputField value="{!Contact.Salutation}"> 
             <apex:actionSupport event="onchange" reRender="pb"/>
        </apex:inputField>
    </apex:pageBlockSection>
    <apex:pageBlockSection id="pbs" rendered="{! Contact.Salutation == 'Mr.' }">  
        <apex:inputField value="{!Contact.Birthdate }"/>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

where the Birthdate appears if "Mr." is selected. If you change it to reRender="pbs" it no longer works.

Related Topic