You need to sign in to do that
Don't have an account?

How to auto-select a selectList option from the controller
Using the code below, how do I select the selectOption who's id matches a pre-defined value?
if (this.site != null)
{
for (market__c m : [SELECT id, name FROM market__c WHERE state__c = :siteState])
{
markets.add(new selectOption(m.id, m.name));
}
}
I want the selectOption to be selected by default if m.id = :siteMarket.
Thank you for any help.
Presumably you have a controller property that contains the selected item from the list?
That being the case, you simply assign it the value of the id from the list.
E.g.
public String selectedId {get; set;}
...
selectedId=siteMarket;
All Answers
Presumably you have a controller property that contains the selected item from the list?
That being the case, you simply assign it the value of the id from the list.
E.g.
public String selectedId {get; set;}
...
selectedId=siteMarket;
Thank you for the quick reply Bob. I really appreciate your help.
I'm not entirely sure where the code you provided needs to be added. As a .net developer I expected to see something that is directly below my loop of adding selectOptions. Since that was obviously a poor assumption I'm struggling to understand where to place the code you provided.
I am guessing the "public String selectedid {get; set;}" would be it's own function, but I can't even guess where to place the other code. Is that part of my existing function?
Thank you again for all of your help. Sorry for the silly questions. Someday it will make more sense to me.
On your page, where you are using the apex:selectList component, you should have a value attribute that binds to a controller property - this is where selectedId would fit in - the name is one I made up - hopefully you already have one.
As long as you set the controller property to the pre-selected id, it will show up as selected in the page.
Perfect! Thank you very much for all of your help. It's working as expected.