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
Srinivas NimmagaddaSrinivas Nimmagadda 

How to show/hide different "pageBlockSection" based on SelectList value

Till now I have done the following as shown below
User-added image

Apex Class: Apex_SelectList_ObjectChange.apxc
public class Apex_SelectList_ObjectsChange {
// Account Fields
    public string 	accName		{set;get;}
    public string 	accNumber	{set;get;}
    public double 	accRevenue	{set;get;}
    public string	accPhone	{set;get;}
    public string	accFax		{set;get;}
    public integer	accemployees{set;get;}
    public string	accRating	{set;get;}
    public string 	accownership{set;get;}
    public string	accSource	{set;get;}
    public string	accDesc		{set;get;}
// Contact Fields
    public string	confirstName{set;get;}
    public string	conlastName	{set;get;}
    public string	conPhone	{set;get;}
    public string	conMobPhone	{set;get;}
    public string	conTitle	{set;get;}
    public date		conBirthdate{set;get;}
    public string	conEmail	{set;get;}
    public string	conLeadSource{set;get;}
// Opportunity Fields
    public string	oppName		{set;get;}
    public string	oppLeadSource{set;get;}
    public string	oppStage	{set;get;}
    public string	oppType		{set;get;}
    public double	oppAmount	{set;get;}
    public integer	oppQuantity	{set;get;}
    public date		oppCloseDate{set;get;}
    public string	selectedVal	{set;get;} // This is to hold the Selected Value, Id is in here

    // Declare List Options     
    public List<SelectOption> getObjectSelected(){
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--None--','--None--'));
//        options.add(new SelectOption('Lead','Lead'));
        options.add(new SelectOption('Account','Account'));
        options.add(new SelectOption('Contact','Contact'));
        options.add(new SelectOption('Opportunity','Opportunity'));
        return options;
    }
    
    public PageReference create(){
        pageReference p;
        Apexpages.Message msg;
        try{
            Account a = new Account();
                a.Name 				= accName;
                a.AccountNumber 	= accNumber;
                a.AnnualRevenue 	= accRevenue;
                a.Phone 			= accPhone;
                a.Fax				= accFax;
                a.NumberOfEmployees = accemployees;
                a.Rating			= accRating;
                a.Ownership 		= accownership;
                a.AccountSource 	= accSource;
                a.Description 		= accDesc;
            insert a;
            Contact c = new Contact();
            	c.FirstName 		= confirstName;
                c.LastName 			= conlastName;
                c.Phone 			= conPhone;
                c.MobilePhone 		= conMobPhone;
                c.Title				= conTitle;
            	c.Birthdate 		= conBirthdate;
                c.Email 			= conEmail;
                c.LeadSource 		= conLeadSource;
            insert c;
            Opportunity o = new Opportunity();
            	o.Name 				= oppName;
                o.LeadSource 		= oppLeadSource;
                o.StageName 		= oppStage;
                o.Type 				= oppType;
                o.Amount 			= oppAmount;
                o.TotalOpportunityQuantity = oppQuantity;
                o.CloseDate 		= oppCloseDate;
            insert o;
            msg = new Apexpages.Message(Apexpages.Severity.CONFIRM,'Record Inserted Successfully');
        }catch(Exception e){
            msg = new Apexpages.Message(Apexpages.Severity.ERROR,'Error Reason is: '+e.getCause());
        }
        return p;
    }
    public void cancel(){
        accName			= null;
        accNumber		= null;
        accRevenue		= null;
        accPhone		= null;
        accFax			= null;
        accemployees	= null;
        accRating		= null;
        accownership	= null;
        accSource		= null;
        accDesc			= null;
        confirstName	= null;
        conlastName		= null;
        conPhone		= null;
        conTitle		= null;
        conMobPhone		= null;
        conBirthdate	= null;
        conEmail		= null;
        conLeadSource	= null;
        oppName			= null;
        oppLeadSource	= null;
        oppStage		= null;
        oppType			= null;
        oppAmount		= null;
        oppQuantity		= null;
        oppCloseDate	= null;
        selectedVal		= null;
    }
    
}
Visualforce Page: Apex_SelectList_ObjectsChange.vfp
<apex:page controller="Apex_SelectList_ObjectsChange" lightningStylesheets="true">
    <apex:sectionHeader title="Select List Options" subtitle="Swap Objects from List Selection" help="needHelp?" printUrl="Object Swap"/>
    <apex:form id="fm">
    	<apex:pageBlock id="pb">
            <apex:pageMessages />
            <apex:pageBlockButtons location="top"><b>Please Select an Object: </b>
                <apex:selectList value="{!selectedVal}" size="1" multiselect="false">
                    <apex:selectOptions value="{!ObjectSelected}" />
                </apex:selectList>
                <apex:commandButton value="Save" action="{!create}" reRender="pb"/>
                <apex:commandButton value="Cancel" action="{!cancel}" reRender="pb"/>                
            </apex:pageBlockButtons>
            
<!--Account Details -->
            <apex:pageBlockSection columns="1">
            	<apex:pageBlock title="Account" mode="mainDetail">
                    <apex:pageBlockSection id="AccountSection">
	                	<apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Number" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Phone" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Fax" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Rating" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Ownership" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Revenue" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Source" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Employees" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Description" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                	</apex:pageBlockSection>
                </apex:pageBlock>
                
<!--Contact Details -->            	
                <apex:pageBlock title="Contact" mode="mainDetail">
                    <apex:pageBlockSection id="ContactSection">
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact First Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Last Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Phone" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Mobile Phone" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Title" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Birthdate" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Email" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Lead Source" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                </apex:pageBlock>
                
<!--Opportunity Details -->            	
                <apex:pageBlock title="Opportunity" mode="mainDetail">
                    <apex:pageBlockSection id="OpportunitySection">
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Lead Source" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Stage" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Type" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Amount" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Quantity" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Close Date" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                	</apex:pageBlockSection>        
                </apex:pageBlock>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

When ever I select a value from the Select List value, then respective pageblocksection must be displayed like below

1. When I select "ACCOUNT" only account records should showup
User-added image
2. When I select "CONTACT" only contact records should showup
User-added image
3. When I select "OPPORTUNITY" only opportunity records should showup
User-added image
Best Answer chosen by Srinivas Nimmagadda
Maharajan CMaharajan C
Try the below changes in your VF Page:

<apex:page controller="Apex_SelectList_ObjectsChange" lightningStylesheets="true">
    <apex:sectionHeader title="Select List Options" subtitle="Swap Objects from List Selection" help="needHelp?" printUrl="Object Swap"/>
    <apex:form id="fm">
        <apex:pageBlock id="pb">
            <apex:pageMessages />
            <apex:pageBlockButtons location="top"><b>Please Select an Object: </b>
                <apex:selectList value="{!selectedVal}" size="1" multiselect="false">
                    <apex:selectOptions value="{!ObjectSelected}" />
                    <apex:actionSupport event="onchange" reRender="acc,con,opp"/>
                </apex:selectList>
                <apex:commandButton value="Save" action="{!create}" reRender="pb"/>
                <apex:commandButton value="Cancel" action="{!cancel}" reRender="pb"/>                
            </apex:pageBlockButtons>
<!--Account Details -->
            <apex:pageBlockSection columns="1">
            <apex:outputPanel id="acc" >
                <apex:pageBlock title="Account" mode="mainDetail" rendered="{!selectedVal == 'Account'}" >

                    <apex:pageBlockSection id="AccountSection">
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Number" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Phone" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Fax" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Rating" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Ownership" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Revenue" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Source" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Employees" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Description" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                </apex:pageBlock></apex:outputPanel>
                
<!--Contact Details -->     
                <apex:outputPanel id="con" >        
                <apex:pageBlock title="Contact" mode="mainDetail"  rendered="{!selectedVal == 'Contact'}">

                    <apex:pageBlockSection id="ContactSection">
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact First Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Last Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Phone" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Mobile Phone" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Title" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Birthdate" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Email" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Lead Source" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                </apex:pageBlock>
                </apex:outputPanel>
                
<!--Opportunity Details --> 
            <apex:outputPanel id="opp" >            
                <apex:pageBlock title="Opportunity" mode="mainDetail" rendered="{!selectedVal == 'Opportunity'}">

                    <apex:pageBlockSection id="OpportunitySection">
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Lead Source" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Stage" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Type" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Amount" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Quantity" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Close Date" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>        
                </apex:pageBlock>
                </apex:outputPanel>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks,
Maharajan.C

All Answers

MagulanDuraipandianMagulanDuraipandian
You have to use rendered attrbute. Check this for example - http://www.infallibletechie.com/2013/04/hide-and-show-example-using-apex-in.html
Maharajan CMaharajan C
Try the below changes in your VF Page:

<apex:page controller="Apex_SelectList_ObjectsChange" lightningStylesheets="true">
    <apex:sectionHeader title="Select List Options" subtitle="Swap Objects from List Selection" help="needHelp?" printUrl="Object Swap"/>
    <apex:form id="fm">
        <apex:pageBlock id="pb">
            <apex:pageMessages />
            <apex:pageBlockButtons location="top"><b>Please Select an Object: </b>
                <apex:selectList value="{!selectedVal}" size="1" multiselect="false">
                    <apex:selectOptions value="{!ObjectSelected}" />
                    <apex:actionSupport event="onchange" reRender="acc,con,opp"/>
                </apex:selectList>
                <apex:commandButton value="Save" action="{!create}" reRender="pb"/>
                <apex:commandButton value="Cancel" action="{!cancel}" reRender="pb"/>                
            </apex:pageBlockButtons>
<!--Account Details -->
            <apex:pageBlockSection columns="1">
            <apex:outputPanel id="acc" >
                <apex:pageBlock title="Account" mode="mainDetail" rendered="{!selectedVal == 'Account'}" >

                    <apex:pageBlockSection id="AccountSection">
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Number" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Phone" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Fax" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Rating" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Ownership" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Revenue" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Source" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Employees" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Account Description" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                </apex:pageBlock></apex:outputPanel>
                
<!--Contact Details -->     
                <apex:outputPanel id="con" >        
                <apex:pageBlock title="Contact" mode="mainDetail"  rendered="{!selectedVal == 'Contact'}">

                    <apex:pageBlockSection id="ContactSection">
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact First Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Last Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Phone" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Mobile Phone" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Title" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Birthdate" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Email" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Contact Lead Source" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>
                </apex:pageBlock>
                </apex:outputPanel>
                
<!--Opportunity Details --> 
            <apex:outputPanel id="opp" >            
                <apex:pageBlock title="Opportunity" mode="mainDetail" rendered="{!selectedVal == 'Opportunity'}">

                    <apex:pageBlockSection id="OpportunitySection">
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Name" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Lead Source" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Stage" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Type" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Amount" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Quantity" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                        <apex:pageBlockSectionItem >
                            <apex:outputLabel value="Opportunity Close Date" />
                            <apex:inputText label=""/>
                        </apex:pageBlockSectionItem>
                    </apex:pageBlockSection>        
                </apex:pageBlock>
                </apex:outputPanel>
                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Thanks,
Maharajan.C
This was selected as the best answer
Srinivas NimmagaddaSrinivas Nimmagadda
Thank you Magulan & Maharajan.

Maharajan your tweek in the VFpage helped me on achieving this.

I have one more extention to this. 

When I insert values and click SAVE. Then it has to take me to that created record

If I fill ACCOUNT and switched to CONTACT and fill contact and save, then it has to create CONTACT but not ACCOUNT. (So basically what ever 

1. If I fill ACCOUNT details, and click SAVE then it has to take me to that newly created account.

2. If I fill CONTACT details, and click SAVE then it has to take me to that newly created contact.

3. If I fill Opportunity details, and click SAVE then it has to take me to that newly created opportunity.

4. If I click CANCEL, it has to null all the values that were filled on that page.