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
Lakshmi SLakshmi S 

How Visualforce page block section hide & enable based on custom object picklist value ?

Hi Team,

How can we do Visualforce page block section hide & enable based on custom object picklist value ?


Thanks,
Lakshminarasimha.

 
DevADSDevADS
Hey Lakshmi,

You can use a boolean variable with rendered attribute and toggle the value based on action.

Do post here if you've any further questions.

Happy Coding!
Lakshmi SLakshmi S
Hi DevADS,


Thanks for your reply.

I have one custom picklist with 3 options, i have created 3 pageblocksections with diffrent fields. If i select picklist value need to enable appropriate pageblocksection.
Please let me know how can we achieve this.


Thanks
Lakshmi.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Lakshmi,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page standardController="Contact" extensions="HideSectionBasedOnPicklistC">
    <apex:form >
        <apex:pageBlock > 
            <apex:actionRegion >
                <apex:selectList value="{!pickchoice}" multiselect="false" size="1">
                    <apex:selectOptions value="{!items}"/>
                    <apex:actionSupport event="onchange" rerender="out1,out2"/>
                </apex:selectList>
            </apex:actionRegion>            
            <br/><br/>
            <apex:pageBlockSection id="out1" >
                <apex:outputPanel rendered="{!IF(pickchoice== 'NRI' && pickchoice!= 'None', true , false)}">                    
                    <apex:outputLabel value="First Name" /><br/>
                    <apex:inputField value="{!contact.FirstName}" /><br/><br/>
                    <apex:outputLabel value="Last Name" /><br/>
                    <apex:inputField value="{!contact.LastName}" />
                </apex:outputPanel>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection id="out2" >
                <apex:outputPanel rendered="{!IF(pickchoice== 'Non-NRI' && pickchoice!= 'None', true , false)}">
                    <apex:outputLabel value="Email" /><br/>
                    <apex:inputField value="{!contact.Email}" /><br/><br/>
                    <apex:outputLabel value="Phone" /><br/>
                    <apex:inputField value="{!contact.Phone}" />
                </apex:outputPanel>
            </apex:pageBlockSection>        
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class HideSectionBasedOnPicklistC {
    
    public String Pickchoice {get;set;}
    
     public HideSectionBasedOnPicklistC(ApexPages.StandardController controller) {

    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('None','None'));
        options.add(new SelectOption('NRI','NRI'));
        options.add(new SelectOption('Non-NRI','Non-NRI'));
        return options;
    }
}

Screenshots:

User-added image

User-added image

User-added image

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Lakshmi SLakshmi S
Hi Khan Anas,

Thanks for your reply.

I am not using picklist options like below.

public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('None','None'));
options.add(new SelectOption('NRI','NRI'));
options.add(new SelectOption('Non-NRI','Non-NRI'));
return options;
}

I am using picklilst options directly from Standard controller Custom object.
Example :
<apex:inputfield value="{!Account.industry}"/>

based on this picklist i want to display pageblock sections accordingly, if i deselect picklist options need to disable the pageblock sections.

Please advise ....


Thanks,
Lakshmi.
DevADSDevADS
HI Laxmi,

Find below the sample code -
<apex:page standardController="Account">
    
    <apex:form id="theForm">
        
        <apex:pageBlock>
            <apex:pageblockSection>
                <apex:inputField value="{!account.Industry__c}">
                    <apex:actionSupport event="onchange" reRender="theForm"/>
                </apex:inputField>
            </apex:pageblockSection>
        </apex:pageBlock>
        
        <apex:pageblock>
            <apex:pageblockSection rendered="{!account.Industry__c = 'Section 1'}">
                <apex:inputField value="{!account.Name}"/>
            </apex:pageblockSection>
            
             <apex:pageblockSection rendered="{!account.Industry__c = 'Section 2'}">
                <apex:inputField value="{!account.Phone}"/>
            </apex:pageblockSection>
            
            <apex:pageblockSection rendered="{!account.Industry__c = 'Section 3'}">
                <apex:inputField value="{!account.Website}"/>
            </apex:pageblockSection>
        </apex:pageblock>
        
    </apex:form>
    
</apex:page>

See below screenshots for the execution -
1. Page on load
User-added image
2.When Industry = 'Section 1'
User-added image
3.When Industry = 'Section 2'
User-added image

Post here if you have any further questions.

Happy Coding!!

​​​​​​​
Lakshmi SLakshmi S
Hi DevADS,

Thanks for your reply.
It is working but, we lost the remaining fields data when we change the picklist options, because entire page is refreshing.Please advise any alternative solution.


HI Khan Anas,

Thanks for your reply.
It is working but one issue, if we select picklist option it show related pageblock section, again if we select ' None ' option existing selected pageblock section not hidden.
Please advise how can we resolve this issue.
I have one more qurey, this functionality is not supported in detail page inline edit.is it possible ?

Thanks,
Lakshmi S.
 
DevADSDevADS
Hey Laxmi,

It should not refresh the page becasue we are just rerendering the form which would make AJAX request and update the state. 

Make sure, you are not calling any pagereference methods or having any javascript called "oncomplete" of actionsupport.

Happy Coding!