3

my picklist value comes from an object but upon page load, i want to have '--select student--' as the default value of the picklist.

controller snippet

public List<selectOption> getsNames(){

        List<selectOption> options = new List<selectOption>(); 
        options.add(new selectOption('', '--Select Student--'));
        for(Parent__c p : [Select Id, Name, First_Name__c, (Select Id, First_Name__c, Name from Students__r) 
                           from Parent__c WHERE Name = 'Delgado']){
                               for(Student__c stud : p.Students__r){
                                   selectedSId = stud.Id;    
                                   options.add(new selectOption(stud.Id, stud.First_Name__c + ' ' + stud.Name));
                               }
                           }
        return options;

    }

in this code, the '--select student--' appears on the top but on page load, the name of the student appears. how can i make it a default that '--select student--' is the value of the picklist?

VFP

    <apex:pageBlock id="chooseStudentPageBlock" >            
<div>    
<apex:outputLabel style="margin-left: 55px;">Select Student:</apex:outputLabel>
<apex:actionRegion>
   <apex:selectList size="1" style="margin-left: 5px;" value="{!selectedSId}" >
         <apex:actionSupport event="onclick"   reRender="teacher"/>
                <apex:selectOptions value="{!SNames}"></apex:selectOptions>
                      <apex:actionSupport event="onchange"  action="{!fetchTeacherInfo}" reRender="teacherBlock"/>
     </apex:selectList>
    </apex:actionRegion>
 </div>                 
 </apex:pageBlock> 
3
  • have you tried adding javascript on pageload ?
    – Rainmaker
    Feb 24, 2016 at 3:29
  • no. is there any other way? Feb 24, 2016 at 3:33
  • have you tried putting options.add(new selectOption('', '--Select Student--')); before return options;
    – Micz Domz
    Feb 24, 2016 at 10:11

2 Answers 2

3

You can simply default by assigning the default value to your "selectedSId" variable in your class constructor.Try the below code

   public classconstructor(){ //This is your class constructor
      getsNames();
      selectedSId = ''; 
   }
   public List<selectOption> getsNames(){

    List<selectOption> options = new List<selectOption>(); 
    options.add(new selectOption('', '--Select Student--'));
    for(Parent__c p : [Select Id, Name, First_Name__c, (Select Id, First_Name__c, Name from Students__r) 
                       from Parent__c WHERE Name = 'Delgado']){
        for(Student__c stud : p.Students__r){   
                options.add(new selectOption(stud.Id, stud.First_Name__c + ' ' + stud.Name));
        }
      }
    return options;

}
0
<apex:outputText value="{!selectedSId}" style="display:none;"></apex:outputText>

returns the first value on the picklist. thank you

1
  • Gelay.. No this is not an answer.. I really don't know why your code is not working what suppose to.. But for workaround I did this so for now your issue resolved
    – Ratan Paul
    Feb 24, 2016 at 6:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .