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
yogesh_patilyogesh_patil 

how to avoid a visualforce page from getting refreshed after calling apex method on button click?

this is my visualforce code...
Actually i want to show the loading image button on click of upload attachment  button
On click of select Attachment the apex method is being called which makes the page to rfresh...
i want to avoid the page refresh...
and rerender cannot be used on <apex:inputfile>
////////visualforce code /////////

<apex:page standardController="Account" extensions="extendAccountforstandardController">
<apex:form id="frm">
<apex:pageBlock >
<apex:pageblockSection >
<apex:inputFile value="{!objAttachment.body}" fileName="{!objAttachment.name}"/> 
</apex:pageblockSection>
<apex:pageBlockButtons >
<apex:commandButton value="Select Attachment" action="{!selectAttachment}" rerender="none" />
</apex:pageBlockButtons>
</apex:pageBlock>
<apex:actionRegion >
<apex:pageBlock id="pgBlkId" >
<apex:pageBlockSection >
<apex:inputField value="{!objAccount.parentid}"/>

</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:outputpanel id="image">
<apex:commandButton action="{!attachRecord}" value=" upload attachment" reRender="image" status="actStatusId" />
</apex:outputpanel>
 <apex:actionStatus id="actStatusId" >
<apex:facet name="start"  >
<img src="/img/loading.gif" />                    
</apex:facet>
</apex:actionStatus> 
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:actionRegion>
</apex:form>
</apex:page>

///// Apex Code

public class extendAccountforstandardController {

    public PageReference attachRecord() {
    
    System.debug('@@objAccount.id'+objAccount.id);
    
     System.debug('@@objAccount.parentId'+objAccount.parentId);
      
      objAttachment.parentId=objAccount.parentId;
      objAttachment.body=bodyAttachment;
      objAttachment.name=nameAttachment;
      
      if(objAccount.parentid!=null){    
        
            insert objAttachment;
        
        }
        return null;
    }

    public blob bodyAttachment;
    
    public string nameAttachment;
    
    public Account objAccount { get; set; }

    public Attachment objAttachment {get; set;}
    
    public extendAccountforstandardController(ApexPages.StandardController controller) {

        objAccount =new Account();
        objAttachment=new Attachment();

    }
    
    public void selectAttachment(){
    bodyAttachment=objAttachment.body;
    nameAttachment=objAttachment.name;
    System.debug(''+'@@nameAttachment'+nameAttachment);
    }
}

 
ajay rawat 14ajay rawat 14
Hi Yogesh,
Since you have used <form> tag so your page is posting back. You can use onclick="return false;" to avoid form POST.

Thanks
Ajay Rawat
yogesh_patilyogesh_patil
But now  file name and file body values are not available in the apex method?
 
yogesh_patilyogesh_patil
but if i am not using form tag then it would not be possible to get inputs from the page?
ajay rawat 14ajay rawat 14
try
 <apex:commandButton value="Select Attachment" action="{!selectAttachment}" rerender="none" onclick="return false;"/>
yogesh_patilyogesh_patil
Actually we cannot use rerender when dealing with <apex:input file.>
rerender option doesnot work here..
please come acroos with different options..
 
Nikhil M KumarNikhil M Kumar
Try using the button attribute 'type' as 'button'.
Pradeep LandgePradeep Landge
try below code: 
<apex:commandButton value="Select Attachment" action="{!selectAttachment}" rerender="none" oncomplete="return false;"/>