[SalesForce] After calling controller class method page is refreshing

I have developed one vf page in this page i have created some popup form where i am inserting some data.In my page i have created 4 buttons if we click on one of the button it will display one popup form. In one popup form i am calling add method of controller class for some additional functionality before saving. Now my question is after calling controller class method my page is getting refresh so popup is not getting to get same popup i have to click on specific button.How i can call controller class method and came back to same popup in my page.
Example:

<apex:page controller="controllermethod">
 <apex:form >
  <apex:pageBlock mode="edit"><br/><br/>
        <div class="row">
       <apex:outputLabel styleClass="label" value="Project Name " /> 
       <apex:inputField styleClass="field" value="{!newproj.Name}"/>
       </div>
       <div class="row">
       <apex:outputLabel styleClass="label" value="Start Date "/> 
       <apex:inputField styleClass="field" value="{!newproj.Start_Date__c}"/>
        </div>
        <div class="row">
       <apex:outputLabel styleClass="label" value="Customer " /> 
       <apex:inputField styleClass="field" value="{!newproj.Customer__c}"/>
       </div>
       <apex:repeat value="{!moduleList}" var="module">
      <div class="row">
       <apex:outputLabel styleClass="label" value="Module " id="test"/> 
       <apex:inputField styleClass="field" value="{!module.Name}"/>
       </div> 
       <div class="row">
       <apex:outputLabel styleClass="label" value="Vendor " id="test1"/> 
       <apex:inputField styleClass="field" value="{!module.Vendor__c}"/>
       </div> 
      </apex:repeat> 

      <apex:commandButton value="Save" action="{!savecreatenew}"/>
      <apex:commandButton value="Cancel" action="{!closePopup}" />

       <apex:commandButton value="Add" action="{!Add}"/>

        </apex:pageBlock>   
        </apex:form>
</apex:page>

Controller:

public class controllermethod 
{
   public  Project__c newproj{get;set;}
   public Module__c  module{get;set;}
   public List<Module__c> moduleList{get;set;}

    public controllermethod()
    {
        moduleList=new List<Module__c>();
         newproj = new Project__c();
         module=new Module__c();
         moduleList.add(module);
    }
    public PageReference closePopup() 
    {
        return null;
    }


  public void Add()
  { 

      Module__c module=new Module__c(); 
      moduleList.add(module); 
  }

    public PageReference savecreatenew() 
    {
        insert  newproj; 
        for (Module__c module :moduleList) {
            module.Project__c = String.valueOf(newproj.get('Id'));
        } 
        insert moduleList; 

    module.clear();
    moduleList.clear();
    newproj.clear() ;
        return null;
    }


}

Best Answer

If you are using a command button without reRender atribute the page will be refreshed after click on the button. Try to use reRender="none" attribute to prevent it.
Another solution is to use some of the javascript: onclick="return false;"

<apex:commandButton value="Add" action="{!Add}" reRender="none"/>

or

<apex:commandButton value="Add" onclick="openMyPopup(); return false;"/>

Do NOT call apex void methods from the command button. Instead use PageReference methods:

public PageReference Add(){ 
    Module__c module=new Module__c(); 
    moduleList.add(module); 
    return null;
}
Related Topic