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
Jon Mountjoy_Jon Mountjoy_ 

How to set default button in a radiobutton component?

Hi

I'm using a simple standard selectRadio Visualforce component, with selectOptions inside, just like that documented in the Visualforce developer's guide (and online help). However, I can't find any attribute on either of these components that lets me specify the default button.

In particular, I display a rating, 1 2 3 4 5. Currently it defaults to 5, which is fatal.

How do I specify which option will be the default? Am I missing something?

Thanks!
Jon
Mark YoungMark Young
Hi Jon,
 
The selectOption with a value corresponding to the bound object of the selectRadio should be selected (and I thought nothing if it was null).  Prepopulate the value before displaying the radio.
 
Code:
<apex:form>
  <apex:selectRadio value="{!currentRating}">
    <apex:selectOption itemValue="1" itemLabel="1" />
    <apex:selectOption itemValue="2" itemLabel="2" />
  </apex:selectRadio>
</apex:form>

/*** Controller ***/
public Integer CurrentRating { get; set; }
{
   CurrentRating = 1;
}

 
Mark
Jon Mountjoy_Jon Mountjoy_
Hi Mark

I'm not sure if your answer is the answer to my question :-)

I'm trying to determine how to set up which of the options is selected by default, when the page is rendered. ie. in HTML you can have "checked" as an attribute of one of the items, and it will be automatically checked.

Regards,
Jon
Mark YoungMark Young

Hi Jon,

I've just run up a test using the code I previously included.  There's not really any such thing as a 'default' assuming the selectOption is bound to a field / property - the option that is checked will always reflect the current value of the bound field / property.

So if CurrentRating is null then none of the radios will be checked, and if CurrentRating is set to 2 then the second option will be checked etc...  If binding to a new object then make sure field defaults are set, or override the constructor to prepopulate values.

What type of field are you binding to?
 
Mark


Message Edited by Mark Young on 09-11-2008 01:30 PM
Jon Mountjoy_Jon Mountjoy_
Ahhhh.  <smacks forehead>

Now I understand you - I simply have to change the value returned by CurrentRating() (in your example) to change the default value selected.  Excellent.

Thanks!
Jon