You need to sign in to do that
Don't have an account?

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:
Can someone explain me why this code does not work, and maybe offer another solution?
thanks!!
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 test1When i try this code, for both bottuns, str does not receive the new value (remains empty).
{
string str;
public void test2()
{
str='world';
}
public string getStr()
{
return str;
}
public void setStr(string str1)
{
str = str1;
}
}
Can someone explain me why this code does not work, and maybe offer another solution?
thanks!!
If no action is provided the page would just simply get refreshed!
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
Hi,
I fixed the code you submited based on the comments by Sam.arj and dchasman with both of whom I agree.
Try this:
and
Hope it helps...
Message Edited by TehNrd on 01-16-2009 01:41 PM
thanks a lot for your help, now it works!:)