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
prady-cmprady-cm 

whole page getting refeshed on click of save button

I have a VF page with a save button. i also have a apex:message in a outputpanel

 

<apex:pageBlockButtons id="blockbuttons">
          <apex:commandButton value="Save"  onclick="performValidation()" id="saveid"/>
          <apex:commandButton value="Cancel" action="{!ok}" />
                   
      </apex:pageBlockButtons>
 <apex:outputpanel id="mess">
 <apex:pageMessages />
 </apex:outputpanel>
 
  <apex:actionFunction name="save" action="{!Save}" rerender="mess"/>

<script  language="javascript" type="text/javascript">
        function performValidation()
        {
        Do something
        save();
        }

 

 In my controller i am validating some values and throwing an apex messages

           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error'));

 This message doesnt seem to be getting displayed. The whole page gets refreshed after the save method is called and probably the messages are displayed. Is there anything that can be done to refresh only the output panel and not the whole page.

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You have a race condition going on here, as your commandbutton is posting the form back but so is the actionfunction.  If you want your javascript to be responsible for the page postback, you need to make you return false after executing the javascript, which tells the browser to cancel the default form submission behaviou

 

Should be as simple as:

 

<apex:commandButton value="Save"  onclick="performValidation(); return false;" id="saveid"/>

 

All Answers

bob_buzzardbob_buzzard

You have a race condition going on here, as your commandbutton is posting the form back but so is the actionfunction.  If you want your javascript to be responsible for the page postback, you need to make you return false after executing the javascript, which tells the browser to cancel the default form submission behaviou

 

Should be as simple as:

 

<apex:commandButton value="Save"  onclick="performValidation(); return false;" id="saveid"/>

 

This was selected as the best answer
prady-cmprady-cm

OMG!!!

Bob!!! you are GOD...

 

I burnt so many hours trying to find out what was causing it...

 

Thanks a ton

Chandan3955Chandan3955

Thanks Bob..!! :smileyhappy: