2

So I'm currently working off the example for apex:actionSupport given in the VF dev docs. All is fine with the example. I now am looking to have a button click initiate the refresh of the counter variable. I changed the VF page to the following:

<apex:page controller="testingController" showHeader="false" sidebar="false">
    <apex:form>
        <apex:outputpanel id="counter"> 
            <apex:commandButton value="Increment" />
                <apex:actionSupport event="onclick" 
                                action="{!incrementCounter}" 
                                rerender="counter" status="counterStatus"/>
            <apex:outputText value="Current Count: {!count}"/> 
        </apex:outputpanel>
        <apex:actionStatus id="counterStatus" 
                           startText=" (incrementing...)" 
                           stopText=" (done)"/>
    </apex:form>
</apex:page>

When I hit the command button I'm seeing that the counter variable increments and displays for a couple ms but then get's reverted back. I believe this might have something to do with order of getters/setters interacting with the AJAX refresh? I could be wrong, but looking for an explanation of why this behavior occurs ( as I'm assuming this is working as designed).

1 Answer 1

2

When use <apex:commandButton/> it actually submits the page. You could see the HTML button actually type is submit.

Refer documentation

A button that is rendered as an HTML input element with the type attribute set to submit, reset, or image, depending on the tag's specified values. The button executes an action defined by a controller, and then either refreshes the current page, or navigates to a different page based on the PageReference variable that is returned by the action.

<input id="thePage:theForm:theButton" type="submit" name="thePage:theForm:theButton" value="Save" />

That's the reason, it is submitting the page and re-initiating the counter

Workable code will be (without actionSupport):

Here with the use of action you can submit the page and increment the counter and use rerender attribute to refresh the counter.

<apex:page controller="exampleCon">
    <apex:form >
        <apex:outputpanel id="counter">
            <apex:commandButton value="Increment"  action="{!incrementCounter}" reRender="text1"/>                   
        </apex:outputpanel>

        <apex:outputText value="Current Count: {!count}" id="text1"/>

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

Otherwise putting action="{!incrementCounter}" in commandbutton will keep the incrementCounter but, it makes no sense to use actionSupport with the commandButton in this scenario.

<apex:outputpanel id="counter">
            <apex:commandButton value="Increment"  action="{!incrementCounter}"/>
            <apex:actionSupport event="onclick" 
                                action="{!incrementCounter}" 
                                rerender="counter" status="counterStatus"/>
            <apex:outputText value="Current Count: {!count}"/> 
        </apex:outputpanel>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .