2

I have use the following code I would like to get the radiobutton value in the apex. I have checked the one option button and click the button (to call the method OptionVlaue()) I have checked the value, but it is null.
How to get the selected radiobutton value in apex?

Apex:

public String optionValue { get; set; }    
    Public void OptionVlaue(){        
        system.debug('<<option value>>'+OptionVlaue);        
}

VF page:

<apex:selectRadio value="{!optionValue}">
       <apex:selectOption itemValue="1"></apex:selectOption>
       <apex:selectOption itemValue="2"> </apex:selectOption>
</apex:selectRadio>    
3

Normally a selected value will assigned to the variable automatically when you submit the form. The syntax is very easy:

  • Define a new variable
  • Create a new select list
  • Submit the form (with an action:support for example)
  • Check selected variable in controller

Apex class:

public with sharing MyClass{

    public String selectedValue { get; set; }    

    public MyClass(){
        // Here initializing the variable. If you want to preselect some radio button  
        // just assign it value to the variable here (eg. selectedValue = '1';)
        selectedValue = '';
    }

    public void checkSelectedValue(){        
        system.debug('Selected value is: ' + selectedValue);        
    } 
}

Visualforce:

<apex:selectRadio value="{!selectedValue}">
    <apex:selectOption itemValue="1"></apex:selectOption>
    <apex:selectOption itemValue="2"> </apex:selectOption>

    <apex:actionSupport event="onchange" 
                        action="{!checkSelectedValue}" 
                        reRender="none">
</apex:selectRadio> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.