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
Pooja ShahPooja Shah 

Reading radio button values in controller class

In the following code, I can read the radio button selected only if I click on the test button and not on the hit of the continue button. I have 4 such radio button lists and hence 4 selected values which i need to read in the controller class. Can someone suggest how to do it?
public class sampleCon {
           String country = null;
           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; }
      public PageReference nextPage()
    {PageReference pr = null;
       pr = Page.test;
       System.debug('country is' +country);
       pr.setredirect(true);
       return pr;
    }}
<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:form >
     <apex:commandButton value="Continue" id="Continue" action="{!nextPage}">
     </apex:commandButton>
     </apex:form>
jwetzlerjwetzler
Your continue button is in a separate form, so when you click Continue, it's going to submit all of the values in the containing form.  In your case, there is nothing to submit.

I think you'll find it to be rare that you need more than one form on your page.  Get rid of that second form and make your first form encompass the whole page.
Pooja ShahPooja Shah
Thanks a lot. It works now. I had to get rid of some pageBlocks as a form can not span across pageBlocks

Do you know how to set default values in the radio button list? I couldnt see the attribute default for radio button.

I always want it to default to the same value.
jwetzlerjwetzler
There's no reason why a form can't span across multiple pageBlocks.  Why do you say that?

You already have yourself setup to have a default value set for your radio button.  you set this.country = null in your controller.

this.country = 'US' should do the trick.


Message Edited by jwetzler on 06-12-2008 04:10 PM