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
alonmalonm 

changing a controller variable when pressing a commandButton

I am trying to achieve a very simple task in a VF page: pressing a button will change the value of a variable of the custom controller of the page, and the new value will appear on the screen. In the sample code below, there are two buttons which i tried, but both don't work. one of them is calling a javascript function (using the actionFunction tag), and the other is calling a method of the controller (using the action attribute).

Here is the VF page code:

<apex:page controller="test1">
    <apex:form >
        <apex:messages></apex:messages>
        <apex:pageBlock>
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Press Here 1" onclick="setStr('world');" status="testStatus"  rerender="thisPage:Block1"/>
            </apex:pageBlockButtons>
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Press Here 2" action="{!test2}" status="testStatus"  rerender="thisPage:Block1"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:actionFunction name="setStr" >
            <apex:param name="firstParam" value="" assignTo="{!str}"/>
        </apex:actionFunction>
        <apex:actionStatus id="testStatus" startText="requesting..."/>
        <apex:pageBlock id="Block1">
            <apex:outputText value="Hello {!str}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

and this is the controller code:
public class test1
{
string str;

public void test2()
{
str='world';
}


public string getStr()
{
return str;
}
public void setStr(string str1)
{
str = str1;
}
}
When i try this code, for both bottuns, str does not receive the new value (remains empty).

Can someone explain me why this code does not work, and maybe offer another solution?

thanks!!
Sam.arjSam.arj
Your actionFunction is missing an attribute!

Code:
<apex:actionFunction name="setStr" action="{!someAction}" >
     <apex:param name="firstParam" value="" assignTo="{!str}"/>
</apex:actionFunction>

 If no action is provided the page would just simply get refreshed!


dchasmandchasman
... and your rerender target of "thisPage:Block1" is bogus (the parent of Block1 is not id'ed as thisPage). As a general rule please get your page working with full page updates (no ajax/partial page updates, rerenders etc) and then layer rerender and oncompletes etc on top. Also, the use of apex:commandButton to call javascript like this is not recommended - just use a simple HTML button instead...
alonmalonm
Hey guys,
thanks for your answers, but i still cant seem to make this simple task work..

Sam: the action attribute of actionFunction is not required, and i saw examples where actionFunction is used without this attribute. As a matter of a fact, my example is based on another code sample here on the discussion board (http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=6371) where a similar task is perforemd, but with radio buttons and not a command button.

Doug: this code had several versions, in this version i forgot to add the id attribute to the page. so now i added it (id="thePage"), but this didnt help.

So any more suggestions would be appriciated!

cheers Alon

andresperezandresperez

Hi,

I fixed the code you submited based on the comments by Sam.arj and dchasman with both of whom I agree.

Try this:

Page:
<apex:page controller="aaTest1">
    <apex:form >
        <apex:messages></apex:messages>
        <apex:pageBlock>
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Press Here 1" onclick="setStr('world1');" status="testStatus" rerender="thisPage:Block1" />
            </apex:pageBlockButtons>
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Press Here 2" action="{!test2}" status="testStatus" rerender="Block1"> 
                    <apex:param name="firstParam" value="world2"/>
                </apex:commandButton>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        <apex:actionFunction name="setStr" action="{!test2}" status="testStatus" rerender="Block1">
            <apex:param name="firstParam" value="" />
        </apex:actionFunction>
        <apex:pageBlock id="Block1">
            <apex:outputText value="{!str}"/>
        </apex:pageBlock>
        <apex:actionStatus id="testStatus" startText="requesting..."/>
    </apex:form>
</apex:page>

and

APEX Class:
public class aaTest1 {
    string str = 'Before';
    public void test2() {
        str = ApexPages.currentPage().getParameters().get('firstParam');
    }
    public string getStr() {
        return str;
    }
}

Hope it helps...
TehNrdTehNrd
If you are just trying to understand the basics this should help.

Code:
public class test1{
    public String str {get; set;}
    
    public void test2(){
        system.debug('whoohoo');
  //could also set str value here
  // str = 'woohoo';
    }
}



<apex:page controller="test1">
    <apex:form >
        <apex:messages></apex:messages>
        <apex:pageBlock>
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Press Here 1" action="{!test2} status="testStatus"  erender="Block1"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
     
        <apex:actionStatus id="testStatus" startText="requesting..."/>
        <apex:pageBlock id="Block1">
   <apex:inputText value="{!str}"/>
            <apex:outputText value="Hello {!str}"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 



Message Edited by TehNrd on 01-16-2009 01:41 PM
alonmalonm
Hi all,
thanks a lot for your help, now it works!:)