[SalesForce] reRender is not working

i write a simple visualforce page

<apex:page controller="Sample1">
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Congratulations</h1>
  This is your new Page
  <apex:outputText id="outText"value="Hello World" rendered= "{! xx}"/>
  <apex:form>
  <apex:commandButton value="Click" action="{!changeValue}" rerender="outText"/>
  </apex:form>
  <!-- End Default Content REMOVE THIS -->
</apex:page>

and controller code is

public class Sample1{
public Boolean xx{get;set;}
public Sample1(){
xx=true;
}

public PageReference changeValue(){
xx=false;
return null;
}
}

but when i click the button nothing happens Hello World still renders.Please explain why rerender is not working ??

Best Answer

The element that you re-render seems to have to be a container of the outputText; this works:

<apex:page controller="Sample1">
  <apex:outputPanel layout="block" id="outText">
      <apex:outputText value="Hello World" rendered= "{!xx}"/>
  </apex:outputPanel>
  <apex:form>
      <apex:commandButton value="Click" action="{!changeValue}" rerender="outText"/>
  </apex:form>
</apex:page>

The "Bob Buzzard Blog" post Visualforce Re-rendering Woes suggests the reasoning behing this design:

... containing output panel will always be present on the page and thus the Ajax request will be able to update it on completion. The inner component may or may not be rendered depending on ...

Related Topic