8

I want to rerender a pageBlock by checking whether the inputField is null. If it is not null, then it should rerender the pageblock. I wrote the code, but the page block which needs to be rendered is not getting displayed.

my code

<apex:page standardController="Event" extensions="CVFC_EventPage_ctrl_Dev" >
 <apex:form >  
  <apex:pageBlock >
     <apex:pageBlockSection >
         <apex:inputField value="{!Event.Ownerid}" />
         <apex:inputField value="{!Event.Subject}"/>             
         <apex:inputField value="{!Event.IsAllDayEvent}"/>
         <apex:inputField value="{!Event.StartDateTime}"/>
         <apex:inputField value="{!Event.EndDateTime}"/>
         <apex:inputField value="{!Event.WhatId}"/>
         <apex:inputField value="{!Event.WhoId}"/>
         <apex:inputField value="{!Event.IsPrivate}"/>
         <apex:inputField value="{!Event.Location}"/>                         
     </apex:pageBlockSection>         
     <apex:pageblockButtons >
         <apex:commandButton value="Save" action="{!saveAndNew}"/>
     </apex:pageblockButtons>         
  </apex:pageBlock>

  <apex:pageBlock rendered="{!Event.Ownerid != ''}">
     <apex:pageBlockSection id="Event_view"  title="Event View" >
         <apex:inputField value="{!Event.Subject}"/>
     </apex:pageBlockSection>
  </apex:pageBlock>

When I remove rendered="{!Event.Ownerid != ''}" from the pageBlock it gets displayed, otherwise it does not. Can anybody tell me what might be the issue?

1
  • AnujRaj, have you considerd selecting Jesse's answer as the solution ? Commented Jul 22, 2013 at 7:48

1 Answer 1

10

You do not have any code in your example that will rerender anything. All you have is the rendered attribute which is only determined when the page is originally loaded. You should try using the apex:actionSupport Visualforce functionality. Your page would look something like:

<apex:page standardController="Event" extensions="CVFC_EventPage_ctrl_Dev" >
 <apex:form >  
  <apex:pageBlock >
     <apex:pageBlockSection >
         <apex:inputField value="{!Event.Ownerid}">
             <apex:actionSupport event="onchange"
                                rerender="blockToRerender"/>
         </apex:inputField>
         <apex:inputField value="{!Event.Subject}"/>             
         <apex:inputField value="{!Event.IsAllDayEvent}"/>
         <apex:inputField value="{!Event.StartDateTime}"/>
         <apex:inputField value="{!Event.EndDateTime}"/>
         <apex:inputField value="{!Event.WhatId}"/>
         <apex:inputField value="{!Event.WhoId}"/>
         <apex:inputField value="{!Event.IsPrivate}"/>
         <apex:inputField value="{!Event.Location}"/>                         
     </apex:pageBlockSection>         
     <apex:pageblockButtons >
         <apex:commandButton value="Save" action="{!saveAndNew}"/>
     </apex:pageblockButtons>         
  </apex:pageBlock>

  <apex:outputPanel id="blockToRerender">
      <apex:pageBlock rendered="{!ISBLANK(Event.OwnerId)}">
         <apex:pageBlockSection id="Event_view"  title="Event View" >
             <apex:inputField value="{!Event.Subject}"/>
         </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:outputPanel>

There are going to be a few issues with this though as this is a lookup field and the Javascript that fires off of it is a bit tricky.

Also, you may have some issues with the OwnerId actually getting set in the controller. I just can't seem to remember right now if these values are posted back to the server from an apex:actionSupport right now or not. Someone feel free to correct me if my assertion that they do get posted is incorrect.

Hopefully this helps you get moving in the right direction.

5
  • 2
    This is a great answer, and in case OwnerId is not properly updating server side you can call an actionFunction onblur of the inputField for equivalent functionality.
    – Phil Rymek
    Commented Mar 4, 2013 at 19:39
  • action support and action function are two methods that I use on the regular. here's a tutorial opfocus.com/2013/02/… Commented Mar 4, 2013 at 22:01
  • @jesse you're example actually won't work, the top-level component of a re-render doesn't have it's rendered attribute re-evaluated, you'd want to wrap the page block in an output panel and rerender that instead Commented Mar 5, 2013 at 3:45
  • 1
    @Ralph That does make sense. I just wrote the code here without actually testing it, so those types of adjustments would definitely have to be made. I'll update my example. Commented Mar 5, 2013 at 17:27
  • Something important to point out in Jesse's example as well is the "ISBLANK" function being used for the rendered attribute. I've seen issues with using != or < > operators in rendered attributes. The ISBLANK VF function is the way to go. More info here: salesforce.com/us/developer/docs/pages/Content/… Commented Mar 6, 2013 at 15:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .