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
Steve_FinlaySteve_Finlay 

How to set the selected radio button?

In the following code sample...

 

<apex:selectRadio value="{!CustomObject__c.CustomField__c}" >

  <apex:selectOption itemValue="true" itemLabel="Yes" />

  <apex:selectOption itemValue="false" itemLabel="No" />

</apex:selectRadio>

 

The second item "No" is selected by default. I want the first item "Yes" to be the one that is, by default, the selected item.

 

Steve

Best Answer chosen by Admin (Salesforce Developers) 
GaneshDGaneshD

Using controller you can have any radio button default selected .


Below example, I have made 'CANADA' country as default selected.

/*** Controller ***/
public class sampleCon {
String country = 'CANADA';

public PageReference test() {
return null;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('US','US'));
options.add(new SelectOption('CANADA','Canada'));
options.add(new SelectOption('MEXICO','Mexico')); return options;
}
public String getCountry() {
return country;
}
public void setCountry(String country) { this.country = country; }
}



<!-- Page: -->
<apex:page controller="sampleCon">
<apex:form >
<apex:selectRadio value="{!country}">
<apex:selectOptions value="{!items}"/>
</apex:selectRadio><p/>
<apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
</apex:form>
<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel >
<p>You have selected:</p>
<apex:outputText value="{!country}"/>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:page>

 

Hope above example helps you.

 

Ganesh

All Answers

GaneshDGaneshD

Using controller you can have any radio button default selected .


Below example, I have made 'CANADA' country as default selected.

/*** Controller ***/
public class sampleCon {
String country = 'CANADA';

public PageReference test() {
return null;
}
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('US','US'));
options.add(new SelectOption('CANADA','Canada'));
options.add(new SelectOption('MEXICO','Mexico')); return options;
}
public String getCountry() {
return country;
}
public void setCountry(String country) { this.country = country; }
}



<!-- Page: -->
<apex:page controller="sampleCon">
<apex:form >
<apex:selectRadio value="{!country}">
<apex:selectOptions value="{!items}"/>
</apex:selectRadio><p/>
<apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
</apex:form>
<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
<apex:facet name="stop">
<apex:outputPanel >
<p>You have selected:</p>
<apex:outputText value="{!country}"/>
</apex:outputPanel>
</apex:facet>
</apex:actionstatus>
</apex:outputPanel>
</apex:page>

 

Hope above example helps you.

 

Ganesh

This was selected as the best answer
Steve_FinlaySteve_Finlay

Ok, thanks, I'll try it out. 

TryCodingTryCoding

hey sfinlay

this is not working in my case

did this work in your problem

if no then please post the alternative solution if you found.

NikuNiku

It works dude Thanks .

SatyanarayanSatyanarayan

{!CustomObject__c.CustomField__c} if value of this custom fileld is true then 'Yes' will be selected.. 

vnbhargavivnbhargavi
You can add layout attribute as "Page direction" for select options display the values vertically.