+ Start a Discussion
mavsmavs 

Multiple buttons

Can we pass parameter from vf page to controller?

 

I have 6 buttons on my VF page which does the same work except on one field. I wanted to use the same page reference to all this 6 buttons.Based on which button is clicked, i want to pass parameter to controller and update one field.

 

for example,

My buttons are Update Phone,Update Address, Update State,Update City

 

<apex:commandButton action="{!update}" value="Update Phone" Here how to pass parameter val='Update Phone' to controller/> <apex:commandButton action="{!update}" value="Update Address" Here how to pass parameter val='Update Address' to controller/> <apex:commandButton action="{!update}" value="Update State" Here how to pass parameter val='Update State' to controller/> <apex:commandButton action="{!update}" value="Update City" Here how to pass parameter val='Update City' to controller/>

 

 

And the controller

 

 

public class MyController { Public PageReference Update(string val) { if(val=='Update Phone') { then do this.... } else if(val=='Update Address) { then do this... } } }

 

 

 

Message Edited by mavs on 04-23-2009 11:30 AM
Best Answer chosen by Admin (Salesforce Developers) 
SaaspertSaaspert
Seems like you have a typo in your code, check param name : it is BottonName on VF page and ButtonName in Apex controller.

All Answers

SaaspertSaaspert

see this thread

 

http://community.salesforce.com/sforce/board/message?board.id=Visualforce&message.id=12138#M12138 

HarmpieHarmpie

<apex:commandButton value="Update Phone" action="{!update}"> <apex:param name="val" value="Update Phone" assignTo="{!val}" /> </apex:commandButton>

 

Make sure to define variable val in your controller, you may then use it ... Public String val { get;set;}

 

mavsmavs

Hello Harmpie and Saaspert -


Thanks for the response.

Actually i did tried that way..but the param value is not getting passed to the controller.
When clicked on the buttons the status value is not being updated....please advise..

what is wrong with the below?

<apex:page tabstyle="Account" controller="MyController"> <apex:form> <apex:commandButton action="{!update}" value="Phone"> <apex:Param name="BottonName" value="Phone" assignTo="{!BottonName}"/> </apex:commandButton> <apex:commandButton action="{!update}" value="State"> <apex:Param name="BottonName" value="State" assignTo="{!BottonName}"/> </apex:commandButton> <apex:commandButton action="{!update}" value="City"> <apex:Param name="BottonName" value="City" assignTo="{!BottonName}"/> </apex:commandButton> </apex:form> </apex:page>

 

public class MyController {

 

Public String ButtonName{ get;set;} public PageReference update() { Id AcctId = ApexPages.currentPage().getparameters().get('id'); String ButtonName=System.CurrentPageReference().getParameters().get('ButtonName'); return redirectResults(AcctId,ButtonName); } public pageReference redirectResults(Id AcctId,String ButtonName) { Account acct=[Select Status__c from Account where id=:AcctId]; if(ButtonName=='Phone') acct.Status__c='Phone is Updated'; else if(ButtonName=='State') acct.Status__c='State is Updated'; else if(ButtonName=='City') acct.Status__c='City is Updated'; update acct;

PageReference myPR = null;
    myPR=new PageReference('/001/o');

if (myPR != null)
    myPR.setRedirect(true);
    return myPR;

}

}

 

Message Edited by mavs on 04-24-2009 08:34 AM
Message Edited by mavs on 04-24-2009 08:36 AM
Message Edited by mavs on 04-24-2009 08:41 AM
SaaspertSaaspert
Seems like you have a typo in your code, check param name : it is BottonName on VF page and ButtonName in Apex controller.
This was selected as the best answer