0

I have a selectOption inside a pageblocksection as follows

 <apex:page standardController="Account" extensions="MyController">
    <apex:form>
       <apex:pageBlock>
          <apex:pageBlockSection>
              <apex:selectList value="{!selectedValue}" multiselect="false" size="1">
                 <apex:selectOptions value="{!optionsList}"/>
                 <apex:actionSupport event="onchange" action="{!updateData}"/>
              </apex:selectList>
           </apex:pageBlockSection>
           ....other page block sections
       </apex:pageBlock>
    </apex:form>
 </apex:page>

The controller for this vf page is as follows:

public class MyController {
   public List<Selectoption> optionsList{get;set;} 
   public String selectedValue{get;set;}

   public MyController() {
      optionsList = new List<Selectoption>();
      optionsList.add(new Selectoption('value1', 'label1'));
      optionsList.add(new Selectoption('value2', 'label2'));
      optionsList.add(new Selectoption('value3', 'label3'));
   }

   public void updateData() {
      System.debug(selectedValue);
   }
}

I need to use the selectedValue for some conditional operations. However I do not get any of the selectOptions values in the selectedValue.

Any idea on what is happening or if I am doing something wrong? Thanks for the help.

2
  • 1
    That code looks alright, I added selectedValue to your page and it updates okay based on the selected value. The selectedValue will be value1 rather than label1 – techtrekker Apr 12 '13 at 21:42
  • try to add the selectedValue to the page and see if it renders on picklist change,(ie) paste this TESTING: {!selectedValue} after the form tag and see what happens when you change the picklist values – Rao Apr 12 '13 at 21:59
1

Two things to try:

Try using a PageReference method instead of void and return null.

Try wrapping your apex:selectList in apex:actionRegion tags

1
  • Thanks, your solutions worked. But I still do not understand what the problem was. I have used selectList and selectOptions many times but never faced this issue. Is it because of the pageBlock, pageBlockSection?? – codeinprogress Apr 15 '13 at 13:34
1

Add rerender attribute and event="onclick" to apex:actionSupport and it should work

1

You can use like

<apex:selectList value="{!selectedValue}" multiselect="false" size="1" onchange="changedValue()"> <apex:selectOptions value="{!optionsList}"/> </apex:selectList> <apex:actionFunction name="changedValue" action="{!updateData}"/>

Thanks, Piyush

0

Your code is looks fine. select list don't take default value unless you change value to other. Here label1 will default value in picklist selected value is null, if you change to label2 then selected value from null to label2. if you change again label1 then it will be label1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.