You need to sign in to do that
Don't have an account?
Controller function fired twice on every postback
Who can explain this?
visual page:
<apex:page controller="TestBinding" >
<apex:form>
<apex:pageBlock title="Test Binding">
<apex:pageblockButtons>
<apex:commandButton value="Login"/>
</apex:pageblockButtons>
<apex:pageblockTable value="{!Accounts}" var ="a">
</apex:pageblockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller
public class TestBinding {
public List<Account> getAccounts(){
List<Account> li = new List<Account>();
return li;
}
}
Apex log:
10:20:11 DEBUG -
***Begining Page Log for /apex/TestBinding 20090901152010.938:
External entry point: returning LIST:SOBJECT:Account from method public LIST:SOBJECT:Account getAccounts() in 0 ms 20090901152010.938:
External entry point: returning LIST:SOBJECT:Account from method public LIST:SOBJECT:Account getAccounts() in 0 ms
Cumulative profiling information: No profiling information for SOQL operations.
No profiling information for SOSL operations.
No profiling information for DML operations. 1 most expensive method invocations: Class.TestBinding: line 2, column 25:
public LIST:SOBJECT:Account getAccounts(): executed 2 times in 0 ms
***Ending Page Log for /apex/TestBinding?core.apexpages.devmode.url=1
Any idea to avoid calling getAccounts twice?
I guess one is init the page, the other is rerender the page.
Is there something like
if ( Apex.PagesPostbackBy('controlID')) then....
?
Thank you
You could try not rendering the form on init, and then updating the render, and refresh the pageblocktable on the submit of your button.
<apex:page controller="TestBinding" > <apex:form> <apex:pageBlock title="Test Binding"> <apex:pageblockButtons> <apex:commandButton value="Login" rerender="bTable" /> </apex:pageblockButtons> <apex:pageblockTable value="{!Accounts}" var ="a" rendered="{!showblock}" id="bTable"> </apex:pageblockTable> </apex:pageBlock> </apex:form> </apex:page>
public class TestBinding { public boolean showblock=false; public List<Account> getAccounts(){ List<Account> li = new List<Account>(); showblock=true; return li; } }
Hi Jim,
Thankyou for your reply,
getAccounts will not be called when page is loading. ( my version it will be fired once.)
But it's still fired twice when you click commandButton.
You're right, misread the post/log.
Could you post your entire apex code?
Hi Harmpie & Jim,
My code are actually kind of complicated. I just simplified it as my first post to proof the concept.
If you add counter,you will see it more clearly.
I tuned my code many times, finally I realized it's force.com framework's problem and no official
safe methods to avoid it.
Thank you