I have a complicated problem so i'm hoping to ask this question generically:
I see a lot of examples that mostly work just fine with SelectList and SelectOptions.
My particular problem is that i can not get the SelectOptions dropdown menu to display the value i want as the "selected" (or saved) value upon the page rendering.
For example, i get a record from the db with "Relationship" = "Sister" and i want the dropdown to show "Sister".
My dropdown menu is built from a SOQL query and would have
"Mother"
"Father"
"Sister"
"Brother"
“Spouse”
and shown in the window (size=1)
But when the page renders, the top value ("Mother") is always shown rather than the "Sister" (3rd value in dropdown – list with size=1 window) no matter how i try to set it (i've tried about 4000 variations).
Can anyone show me an example of this working? Am i misunderstanding something? This seems so simple but i've been battling this for days.
Thank you!!
OK Here's the controller class code.
I load the page with an applicant ID passed in the URL. I go load that applicant record and display it. The "relationship" value is set from that applicant record.
Please note:
-
When i build the dropdown list i do not hardcode the values for the dropdown menu like all the examples out there. I go to my database and pull them out and dynamically build the list from MyDBList__c entries.
I think this is the source of the problem something screwy about this because if i hardcode them, everything is fine!
-
The last item on the page displays the "relationship" correctly in the outputText. The Dropdown, however, does not.
-
I have all the extra containers on the page to mimic what i have in my system…I left them so i know none of those are the problem.
-
applicant.relationship__c is a lookup field to the Name in MyDBList__c (from which the dropdown was built).
Controller
public class TVTestControllerClass
{
public string selectedValue {get;set;}
public Applicant__c applicant;
public ID applicantID {get; set;}
//call ?applicantID=a0N11000004wsh3 to set my applicant
public TVTestControllerClass(){
applicantID = ApexPages.currentPage().getParameters().get('ApplicantID');
}
//the values for the dropdown come from here
public SelectOption[] getselectValues() {
SelectOption[] options = new SelectOption[]{};
for(MyDBList__c oneRelationship : [SELECT Id, Name FROM MyDBList__c]) {
options.add(new SelectOption(
String.valueOf(oneRelationship.Id),oneRelationship.Name));
}
return options;
}
public Applicant__c getApplicant() {
//selectedValue = 'Brother';//default as anything so i know didn't work
//CORRECTION: This should have been something equiv to this...
selectedValue = 'a0H1100000B3bFfEAJ';
applicant =
[SELECT Id, Name, //Name is type Text[80]
applicantFirstName__c,
relationship__c,
relationship__r.Name
FROM Applicant__c
WHERE Id = :applicantID];
//selectedValue = applicant.relationship__r.Name;
//CORRECTION: This SHOULD have been
selectedValue = String.valueOf(applicant.relationship__c);
return applicant;
}//end getApplicant
}//end TVTestControllerClass
Page
<apex:page controller="TVTestControllerClass" sidebar="false" showHeader="false">
<apex:form id="applicantForm" >
<apex:pageBlock >
<apex:outputPanel styleClass="panelWrapper" layout="block">
<apex:outputPanel id="ApplicantDetailPanel">
<apex:pageblocksection id="ApplicantDetail" columns="1" >
<apex:pageBlockSectionItem>
<apex:outputLabel value="First Name" />
<apex:inputField value="{!applicant.applicantFirstName__c}"/>
</apex:pageBlockSectionItem >
<apex:pageBlockSectionItem id="ApplRelBlock">
<apex:outputLabel value="Relationship"/>
<apex:outputPanel layout="block">
<apex:actionRegion >
<apex:selectList value="{!selectedvalue}" size="1" multiselect="false">
<apex:selectOptions value="{!selectValues}"/>
</apex:selectList>
<apex:actionSupport event="onchange" reRender="testingApplR2VID"/>
</apex:actionRegion>
</apex:outputPanel>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem id="testingApplR2VID">
<apex:outputLabel value="selectedValue="/>
<apex:outputText value="{!selectedValue}"/>
</apex:pageBlockSectionItem>
</apex:pageblocksection>
</apex:outputPanel>
</apex:outputPanel>
</apex:pageBlock>
</apex:form>
</apex:page>
Best Answer
You need to set the value that the select list is bound to..
Page
Note in the constructor I set the value of
selectedValue
to the value I want the select list to be set to.Key: That bound value must be set to the VALUE and not the LABEL of the Select Option.
For example:
if the select option was
SelectOption('A','Sister')
then selectedValue needs to be set toA
and notSister
.