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
bohemianguy100bohemianguy100 

commandButton reRender

I'm trying to reRender a pageBlockSection.  On the commandButton I'm using the reRender attribute with the id of the pageBlockSection.  The pageBlockSection is not reRendered.

 

Here is a watered down version of the visualforce page with extraneous code removed for brevity.

 

<apex:page standardController="Contact" extensions="ContactExtension" showheader="false" sidebar="false">    
    <apex:outputPanel >
        <apex:pageMessages id="pageMessages"/>
    </apex:outputPanel>
      <apex:form id="theForm">
           <apex:pageBlock mode="maindetail" id="thePageBlock" >
               <apex:pageBlockButtons location="top">                
                    <apex:commandButton action="{!saveContact}" value="Save" reRender="theSection" />
               </apex:pageBlockButtons>             
                <apex:pageBlockSection columns="2" id="theSection">
                    <apex:inputField value="{!contact.Copy_Address__c}" taborderhint="1" >
                        <apex:actionSupport action="{!changeMade}" event="onchange" rerender="pageMessages"  />
                    </apex:inputField>  
                    <apex:inputField value="{!contact.MailingStreet}" id="mailStreet" taborderhint="2">
                        <apex:actionSupport action="{!changeMade}" event="onchange" rerender="pageMessages"  />
                    </apex:inputField>                                                                                
                </apex:pageBlockSection>   
           </apex:pageBlock>   
      </apex:form>                 
</apex:page>

 I seems like the actionSupport on the inputField is someone interferring with the reRender on the commandButton.

 

Any help is appreciated.

Thanks.

Shiva Ramesh @ xcdhrShiva Ramesh @ xcdhr

Hi bohemianguy100

 

In your page code just change rerender="theForm" like below

 

<apex:commandButton action="{!saveContact}" value="Save" reRender="theForm" />

 

bob_buzzardbob_buzzard

I wouldn't expect the actionsupport to interfere with the rerender from the command button, although there's a very good chance of a race condition there if the action support hasn't completed by the time that you click the command button.  I'd recommend adding an actionstatus that greys out the page and introduces a spinner icon or similar until the request has finished - that way you'll know that the requests aren't overlapping.

bohemianguy100bohemianguy100

Thanks for the reply.

 

I tried using an actionStatus and the same issue occurred.  I click save, I can see the actionStatus showing when the processing starts and ends, but the pageBlockSection does not reRender.  If I close the page and re-open the page, I can see that the values in the pageBlockSection have been updated, so I know that the fields have been updated correctly, but they simply do not refresh when using the reRender AJAX partial page update.

 

The closest I've gotten is setting a flag in the controllerExtension and then rendering some javascript on the visualforce page like this:

 

<apex:outputPanel id="refresh" rendered="true">
 	<apex:outputPanel id="refresh1" rendered="{!refreshPage}">
  		<script>
   			window.location.href = window.location.href;
  		</script>
 	</apex:outputPanel>
</apex:outputPanel>


<apex:page standardController="Contact" extensions="ContactExtension" showheader="false" sidebar="false">    
    <apex:outputPanel >
        <apex:pageMessages id="pageMessages"/>
    </apex:outputPanel>
      <apex:form id="theForm">
           <apex:pageBlock mode="maindetail" id="thePageBlock" >
               <apex:pageBlockButtons location="top">                
                    <apex:commandButton action="{!saveContact}" value="Save" reRender="theSection" />
               </apex:pageBlockButtons>             
                <apex:pageBlockSection columns="2" id="theSection">
                    <apex:inputField value="{!contact.Copy_Address__c}" taborderhint="1" >
                        <apex:actionSupport action="{!changeMade}" event="onchange" rerender="pageMessages"  />
                    </apex:inputField>  
                    <apex:inputField value="{!contact.MailingStreet}" id="mailStreet" taborderhint="2">
                        <apex:actionSupport action="{!changeMade}" event="onchange" rerender="pageMessages"  />
                    </apex:inputField>                                                                                
                </apex:pageBlockSection>   
           </apex:pageBlock>   
      </apex:form>                 
</apex:page>

 This does refresh the page and the updated fields display correctly.  However, I lose my pageMessages.  After clicking save, I display a page message showing the user that the changes were saved successfully.  However, since the javascript reloads the page, I lose the page messages.

 

Please note, the vf page is a contained as a section inside the contact standard page layout.

 

Thanks for the help and let me know if you need any clarification.

bob_buzzardbob_buzzard

Do you have a custom save, or are you using the save method from the standard controller?  If the latter, I think the default behaviour of this is to take you to the view page for the record, so there won't be anything to sensibly change on rerender.

bohemianguy100bohemianguy100

I have a custom save method.  I'm not using the standard controller save method.

 

    public PageReference saveContact() {
        try{
            update contact;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, Label.Contact_Address_Saved_Message));
        }catch(Exception e){
            ApexPages.addMessages(e);
        }
        
        refreshPage = true;
        return null;
    }

 

bob_buzzardbob_buzzard

Hmm.  That should work then.  The other thing I'd be inclined to try is to add the pagemessages to the rerender attribute, just in case there's an error of some sort that is being swallowed (although that doesn't appear to be the case, as your record is updated in the database).

bohemianguy100bohemianguy100

Interesting...if I add rerender="pageMessages" to the commandButton, but now the page doesn't refresh.  I do get the saved page message displaying correctly, but it goes back to the original problem where the page is not refreshed.  It seems like some conflict in the ajax request is occurring.

bob_buzzardbob_buzzard

If you get the page message, that means the rerender has worked. 

 

I'm wondering if there's a disconnect between your view of the contact in the extension and the contact from the standard controller. Is the contact in the extension public? I'm sure I've seen some issues around this a few years ago.

 

What happens if you execute the standard controller's save method rather than updating from the extension controller?

 

Also, do you really need to rerender - is there any reason why you can't just execute that action method and return null (or do you get weird header and sidebar behaviour)?

bohemianguy100bohemianguy100

Yes, the contact in the extension is public.

 

I actually was previously using the standard controller's save method, but suffice it to say, that carried its own set of issues, which is why I refactored the code to use a custom save action method.  It was much less code and a lot more straight-forward to use the custom save method.

 

The current action method in my extension does return null, which I thought would be a simple page refresh, but alas, it doesn't refresh the page and show the updated field values.  Thereby the need for the rerender or some way to refresh the page such as rendering out the javascript to force the page to refresh.

 

This does seem like a crazy way to get the values to display correctly after the update occurs.  The rerender seemed like the best practice, but it doesn't appear to work in the way I expect.  I understand when the page message displays that the rerender worked, but why don't the other components on the page rerender and show their new values?

bob_buzzardbob_buzzard

I suspect its because you have your own contact reference in the extension - as I said, I'm sure I've hit issues with this before.

 

The way I usually do this is to retain a reference to the standard controller rather than to the record.  Something like:

 

public class ContactExtension
{
   private ApexPages.StandardController std;

   public ContactExtension(ApexPages.StandardController stdCtrl)
   {
      std=stdCtrl;
   }

   public PageReference save()
   {
      std.save();

      return null;
   }
}