Reply
Regular Contributor
ryanhca
Posts: 62
Accepted Solution

Basic Show/Hide pageBlockSection not working...

[ Edited ]
I have a page where I have two pageBlockSections that I want to toggle on and off... Here's the page:

 

 

<apex:page standardController="CustomObject__c" extensions="ControllerExt">

<apex:pageMessages id="pageMessages" />

<apex:form id="terminalForm">
<apex:pageBlock>
<apex:pageBlockSection>
<apex:pageBlockSectionItem >
<apex:outputLabel for="type">Type</apex:outputLabel>
<apex:selectList id="type" value="{!type}" size="1">
<apex:selectOptions value="{!typeOptions}" />
<apex:actionSupport event="onchange"
action="{!pageRefreshAction}"
rerender="TypeSection_CC,TypeSection_CH,pageMessages"
</apex:selectList>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>

<apex:outputPanel id="TypeSection_CC" rendered="{!(type == 'option1'||type == 'option2')}">
<apex:pageBlockSection>
<!-- Stuff -->
</apex:pageBlockSection>
</apex:outputPanel>

<apex:outputPanel id="TypeSection_CH" rendered="{!(type == 'option3')}">
<apex:pageBlockSection>
<!-- More Stuff -->
</apex:pageBlockSection>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>

 

As you can see, the sections render depending on the value of "type". The default value of "type" is "option1". So "TypeSection_CC" is displayed when the page loads.

 

The "pageRefreshAction" method just adds a message to the page that displays the value of "type". I've tried making this method void as well, and it makes no difference.

 

 

public PageReference pageRefreshAction() { ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.INFO, 'Type: '+type)); return null; }

 

 

When I choose "option3" from the "type" selectList the pageMessages block is refreshed and shows the value of "type" as "option3", but TypeSection_CC is still displayed and TypeSection_CH is not displayed.

 

Any help?? I don't know why this is causing problems...

 

Message Edited by ryanhca on 02-09-2010 11:55 AM
Trusted Contributor
Rajesh Shah
Posts: 296

Re: Basic Show/Hide pageBlockSection not working...

The problem you are facing is that when the page loads, since type is not option 3, TypeSection_CH is not rendered and hence the page also does not has its id. Next time, when the page tries to rerender it, it cannot find the id since it originally didn't existed for the page.

Solution: move the rendered attribute from outputPanel to pageBlockSection but still rerender the outputPanel. This way, your page will always have the Id, however the pageBlockSection will be rendered only based on the values. 

Regular Contributor
ryanhca
Posts: 62

Re: Basic Show/Hide pageBlockSection not working...

Rajesh - 

 

Thank you! That was driving me crazy. Not only did your solution work, you explained the cause of the problem very clearly. I really appreciate it.

 

Thanks,

Ryan