+ Start a Discussion
Abhishek Singh 88Abhishek Singh 88 

how get selected radiobutton value in constructor

Hi All,
I have a vf page which have two radio button Yes or No. I want selected value in constructor after clicking on submit buton.
here is code.
<apex:page controller="CCO_ScheduleACallController">
<apex:form >
<apex:pageBlock >
<apex:outputLabel style="Bold">Are you able to launch your software or service?</apex:outputLabel>
<apex:selectRadio value="{!radiovalue}">
          <apex:selectOption itemLabel="Yes" itemValue="1"></apex:selectOption>
          <apex:selectOption itemLabel="No" itemValue="2"></apex:selectOption>
      </apex:selectRadio>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!Submit}" value="Submit"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Since in the exisitng contrroler most of the logic is implemented in constructor. And now i have to add condition
IF(selected value=='Yes')
{
//code
}
else
{
// code
}
Dayakar.DDayakar.D
Hi Abhishek,

As constructor will be executed on page load, and constructor will execute only once when we create instance for the class. You cannot achieve above mentioned functionality using constructor, instead use separate method which will execute when you change the options.
Please find below example  
VF Page:

<apex:page controller="CCO_ScheduleACallController">
<apex:form >
<apex:pageBlock >
<apex:outputLabel style="Bold">Are you able to launch your software or service?</apex:outputLabel>
<apex:selectRadio value="{!radiovalue}">
          <apex:selectOptions value="{!Options}"/>
    <apex:actionSupport event="onchange" reRender="outerOutputPanel, op1, op2" action="{!selectOnClick}">
    </apex:actionSupport>
      </apex:selectRadio>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!Submit}" value="Submit"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Controller :
public class CCO_ScheduleACallController {
    public string radiovalue {set;get;}
    public List<SelectOption> getOptions() {
        List<SelectOption> options = new List<SelectOption>(); 
        options.add(new SelectOption('Yes','Yes')); 
        options.add(new SelectOption('No','No')); 
        return options; 
    }
    
    public void Submit()
    {
        //implement your logic
    }
    public void selectOnClick()
    {
        //radiovalue will store the selected option
        system.debug('selectedOption::'+radiovalue);
    }

}
Please let me know, if you need more information.

Best Regards,
Dayakar.D
Ajay K DubediAjay K Dubedi
Hi Abhishek,

Please try the below code. Hope this helps you.

//VF Page

<apex:page controller="TestSelectedOptionClass">
    <apex:form >
        <apex:pageBlock >
            <apex:outputLabel style="Bold">Are you able to launch your software or service?</apex:outputLabel>
            <apex:selectRadio value="{!selectedValue}">
                <apex:selectOption itemLabel="Yes" itemValue="1"></apex:selectOption>
                <apex:selectOption itemLabel="No" itemValue="2"></apex:selectOption>
                <apex:actionSupport event="onchange" action="{!checkSelectedValue}" reRender="none"/>
            </apex:selectRadio>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!Submit}" value="Submit"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

//Controller Class

public class TestSelectedOptionClass {
    public String selectedValue { get; set; }    

    public TestSelectedOptionClass(){
        // 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);        
    } 
    
    public static void Submit()
    {
        
    }
}

The above code will return itemValue in SelectedValue variable .Then you can able to write your condition like this.
IF(selected value=='1')
{
//code
}
else
{
// code
}

Please mark it as best answer if you find helpful.

Thank You
Ajay Dubedi