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
amar joshiamar joshi 

how to Return one particular value that is selected by selectlist compontant [new]

hi all,

we i want to return one particular item(value) that is selected by selectlist compontant

for example in my code there is selectlist compontan also 1 selectoptions compant which contains three value like us,canada,maxico

now i want to return one selected value

suppose if i select canada than i want to return canada

here is my code

page code
Code:
<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectlist value="{!countries}" title="Choose a country" size="1" required="true">
            <apex:selectOptions value="{!items}" />
        </apex:selectlist><br/>
        <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:datatable value="{!countries}" var="c">
                    <apex:column value="{!c}"/>
                    </apex:datatable>  
                </apex:outputPanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:outputPanel>
 </apex:page>
            

 
 controller code
Code:
/*** Controller: ***/
 public class sampleCon {
 
    String[] countries = new String[]{};
 
    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[] getCountries() {
        return countries;
    }
 
    public void setCountries(String[] countries) {
        this.countries = countries;
    }
 

 

JimRaeJimRae
You already have everything you need.  Your selectlist has a getter and setter in your controller, based on "countries", if you reference the object directly, you will get the value set in the page. One thing though, because you are setting up countries as a string array, it appears you were expecting the list to be a multiselect.
If you really only want one result from the selectlist, update your controller like this, and your code will work:
Code:
public class sampleCon {
 
    String countries;
 
    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 getCountries() {
        return countries;
    }
 
    public void setCountries(String countries) {
        this.countries = countries;
    }
 }

 
 
 
 
amar joshiamar joshi
thanks jimrae 

its working thanks a lot buddy