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
Tim ByrnesTim Byrnes 

SelectList size=1 prevents postback

I'm probably overlooking something simple, but in attempting to postback with a apex:selectList with a size set to 1, nothing happens.  Ever.

I got frustrated and started a brand new visualforce page that exactly mimicked the one found here: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_selectList.htm

That worked as expected. (The rerender was correct when selecting items from the selectList.)

Then I tweaked it ever so slightly to look like this:

First, the page:
<apex:page controller="sampleCon">
    <apex:form>
        <apex:selectList value="{!countries}" size="1" >
            <apex:selectOptions value="{!items}"/>
        </apex:selectList><p/>

        <apex:commandButton id="thisButton" value="{!countLabel}" action="{!test}" rerender="thisButton"/>

    </apex:form>
</apex:page>

Now my controller:
public class sampleCon {
        String[] countries = new String[]{};
        public String countLabel {get; set;}

        private Integer iClicks {get; set;}
             
        public sampleCon() {
        	iClicks = 1;
        	countLabel = 'Test ' + iClicks;
        }

        public PageReference test() {
        	iClicks++;
        	countLabel = 'Test ' + iClicks;
            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;
        }      
    }

You'll see that clicking the commandButton does absolutely nothing to the label, nothing changes.  However, if you simply remove the size="1" attribute from the selectList everything will function perfectly. (At least that's what I'm seeing.)

Can anyone else verify, (or refute) this?
 
Best Answer chosen by Tim Byrnes
R Z KhanR Z Khan
Then change  String[] countries = new String[]{}; to a String 

All Answers

R Z KhanR Z Khan
You set the size to 1. That mean countries has to be a string adn not a list of strings
Tim ByrnesTim Byrnes
Nope.  Size is the height of the control.  When size is one the selectList is rendered as a picklist because it's only one entry high.
Tim ByrnesTim Byrnes
Minor updates:
 - Leaving the size attribute in, as long as it is greater than 1 will result in a proper postback and update of the screen.
 - The results are identical across both Chrome and Firefox.
 - This is on CS40, if that matters at all.
 - Altering the API version doesn't change any behavior.  (Started at 36, dropped it one at a time to 32 with no difference.)
 - Adding an apex:actionRegion didn't change anything either.
R Z KhanR Z Khan
try using multiselect=truen if you need a user to populate a list. When you want a user to select only 1 item your value in selectList should be an sObject unless you want them to select multiple tiems
Tim ByrnesTim Byrnes
If multiselect is set to true, then the postback works.  If multiselect is omitted or set to false, postback does not work.

This isn't new territory, it's been done many times by many developers (including myself - I'm following my own template that is proven functioning in other orgs).


 
R Z KhanR Z Khan
So did oyu achieve the desired functionality then with multiselect or you have more questions?
 
Tim ByrnesTim Byrnes
No, multiselect is *not* what I need.  I'm looking to dynamically create a single-select picklist populated with items from a SOQL query that can properly postback to the controller and update the UI.
R Z KhanR Z Khan
Then change  String[] countries = new String[]{}; to a String 
This was selected as the best answer
Tim ByrnesTim Byrnes
You're absolutely right - that worked.  I'm sure I have an equally stupid mistake in my primary page that caused me to go down this rabbit hole.  Thanks, Khan.
R Z KhanR Z Khan
Your welcome. Glad i could help. Please mark question as resolved
Tim ByrnesTim Byrnes
To advertise my simple error to the world (in case if others find themselves chasing similar problems) the selectList in my page that started this whole problem was set up like this:
<apex:selectList value="{!thisQuote.Contact}" size="1" >

Which was the problem to start with.  It works correctly when I bind it to the correct property, like so:
<apex:selectList value="{!thisQuote.ContactId}" size="1" >