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
Mary PustejovskyMary Pustejovsky 

dependent picklist in Visualforce not re-rendering

Hello
I am somewhat new to VF/Apex and I've created a dependent picklist to use in Visualforce. Everything saves fine. When I go to preview, the first picklist appears correctly but the second one does not re-render once the first option is chosen. I am not sure what I am doing wrong. I have searched many boards/blogs etc and tried a couple different things. Any advice would be greatly appreciated. There are actually tons more options in my lists but I figured no one needed to see all of those to understand the issue.

Here is the VF:
<apex:page id="communitiesSelfRegPage" showHeader="true" controller="CommunitiesSelfRegController" title="{!$Label.site.user_registration}">
     <apex:define name="body">  
      <center>
<apex:form id="theForm" forceSSL="true">
                    <apex:pageMessages id="error"/>
                    <apex:panelGrid columns="2" style="margin-top:1em;">
                      <apex:outputLabel value="First Name" for="firstName"/>
                      <apex:inputText required="true" id="firstName" value="{!firstName}" label="First Name"/>
                      <apex:outputLabel value="Last Name" for="lastName"/>
                      <apex:inputText required="true" id="lastName" value="{!lastName}" label="Last Name"/>
                      <apex:outputLabel value="{!$Label.site.community_nickname}" for="communityNickname"/>
                      <apex:inputText required="true" id="communityNickname" value="{!communityNickname}" label="{!$Label.site.community_nickname}"/>
                      <apex:outputLabel value="{!$Label.site.email}" for="email"/>
                      <apex:inputText required="true" id="email" value="{!email}" label="{!$Label.site.email}"/>
                      <apex:outputLabel value="Location" for="Location"/>
                      <apex:selectList value="{!location}" title="Location" multiselect="false" size="3" >
                          <apex:selectOption itemValue="Aerospace Elementary" itemLabel="Aerospace Elementary School"/>
                          <apex:selectOption itemValue="108 Charter Oak" itemLabel="108 Charter Oak"/>
                          <apex:actionSupport event="onchange" reRender="one"/>
                        </apex:selectList>
                        <br/>
                      <apex:outputPanel id="one">
                          <b>Choose a Customer Organization</b>
                          <apex:selectList size="1" value="{!customerOrganization}">
                              <apex:selectOptions value="{!customerOrganization}"/>
                          </apex:selectList>
                        </apex:outputPanel>
                      <apex:outputLabel value="{!$Label.site.password}" for="password"/>
                      <apex:inputSecret id="password" value="{!password}"/>
                      <apex:outputLabel value="{!$Label.site.confirm_password}" for="confirmPassword"/>
                      <apex:inputSecret id="confirmPassword" value="{!confirmPassword}"/>
                      <apex:outputText value=""/>
                      <apex:commandButton action="{!registerUser}" value="{!$Label.site.submit}" id="submit"/>
                    </apex:panelGrid> 
                  <br/>
</apex:form>
     </center>
      <br/>
    </apex:define>

</apex:page>

Here is the controller (at least the relevant part):
public with sharing class CommunitiesSelfRegController {

    public String firstName {get; set;}
    public String lastName {get; set;}
    public String email {get; set;}
    public String phone {get; set;}
    public String password {get; set {password = value == null ? value : value.trim(); } }
    public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
    public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
    public String location {get; set;}
    public String customerOrganization{get;set;}
  
    
    public CommunitiesSelfRegController() {}
    
    public List<SelectOption> getCustomerOrg(){
        List<SelectOption> locs= new List<SelectOption>();
        if(location==null)
            return null;
        else
        {
            if(location.contains('Aerospace Elementary School')){
                locs.add(new SelectOption('Aerospace Elementary School','Aerospace Elementary School'));
                locs.add(new SelectOption('Other','Other'));
            }
            if(location.contains('108 Charter Oak')){
                locs.add(new SelectOption('Student Services', 'Student Services'));
                locs.add(new SelectOption('Other','Other'));
            }
           
                  }
        return locs;
    }

 
Balaji Chowdary GarapatiBalaji Chowdary Garapati

@Mary Pustejovsky:

Couple of things.,

Change your constructor to this:
public CommunitiesSelfRegController() {
location='Aerospace Elementary'; // As it is the first and default one you chose to be on the list and to fetch the correct values for the customer org you need to assign to location field, as the value gets into the location field only when you change the picklist value, until then location will have null, so to avoid it assigning the value
}



and the get method to:
 
public List<SelectOption> getCustomerOrg(){
        List<SelectOption> locs= new List<SelectOption>();
        if(location==null)
            return null;
        else
        {
            if(location.contains('Aerospace Elementary')){
                locs.add(new SelectOption('Aerospace Elementary School','Aerospace Elementary School'));
                locs.add(new SelectOption('Other','Other'));
            }
            if(location.contains('108 Charter Oak')){
                locs.add(new SelectOption('Student Services', 'Student Services'));
                locs.add(new SelectOption('Other','Other'));
            }
           
                  }
        return locs;
    }

Reason is "Aerospace Elementary" is the value you chosen for the label "Aerospace Elementary School" and you need to compare the Values not the labels.

Hope it helps.,

Thanks,
Balaji


 
Mary PustejovskyMary Pustejovsky
Hi Balaji
Thanks for the note. I updated the constructor and the method and it still does not work. Other ideas anyone?

 
Bhanu MaheshBhanu Mahesh
Hi Mary,

Make sure you are populating all the required fields on the page before testing.

Action support will not work if there are any validation errors on the page 

Regards,
Bhanu Mahesh