You need to sign in to do that
Don't have an account?
Toasty02
Where is the value of a selectList stored?
Hello,
I'm new to this Apex stuff, so I'm hoping someone will be able to help me out. I have an Apex page with a selectList, and I'd like to take the value that the use selects and do something with it on the page. The code I have doesn't appear to be working, and I can't figure out why.
This is my controller class:
public class SimplePrintLabelsController {
public String selectedLabelTemplateName {get; set;}
public List<selectOption> labelTemplateList {
get {
List<selectOption> labelTemplateOptions = new List<selectOption>();
labelTemplateOptions.add(new selectOption('', '--Select a label template--'));
for (Label_Template__c lt : [select name from Label_Template__c order by name]) {
labelTemplateOptions.add(new selectOption(lt.name, lt.name));
}
return labelTemplateOptions;
}
set;
}
get {
List<selectOption> labelTemplateOptions = new List<selectOption>();
labelTemplateOptions.add(new selectOption('', '--Select a label template--'));
for (Label_Template__c lt : [select name from Label_Template__c order by name]) {
labelTemplateOptions.add(new selectOption(lt.name, lt.name));
}
return labelTemplateOptions;
}
set;
}
}
And here is the Apex page code:
<apex:page controller="SimplePrintLabelsController">
<script type="text/javascript">
function showAlert() {
alert("You selected " + "{!selectedLabelTemplateName}");
}
</script>
<apex:form >
<apex:pageBlock >
<apex:outputLabel value="Label template: "/>
<apex:selectList id="labelTemplates" value="{!selectedLabelTemplateName}" size="1" onChange="showAlert()">
<apex:selectOptions value="{!labelTemplateList}"/>
</apex:selectList>
</apex:pageBlock>
<apex:pageBlock >
<apex:outputLabel value="You selected: {!selectedLabelTemplateName}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
<script type="text/javascript">
function showAlert() {
alert("You selected " + "{!selectedLabelTemplateName}");
}
</script>
<apex:form >
<apex:pageBlock >
<apex:outputLabel value="Label template: "/>
<apex:selectList id="labelTemplates" value="{!selectedLabelTemplateName}" size="1" onChange="showAlert()">
<apex:selectOptions value="{!labelTemplateList}"/>
</apex:selectList>
</apex:pageBlock>
<apex:pageBlock >
<apex:outputLabel value="You selected: {!selectedLabelTemplateName}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
As you can see, I'm trying to display the selected value in both an output label and in a JavaScript alert. However, the selected value is not showing up in either place. Can anyone tell me what I'm doing wrong here?
Thanks very much!
- Carolyn
Thanks for the reply. I still can't quite put this together...what needs to happen in the controller class for this to work? That is, what should the listBucket method look like?
Thanks,
Carolyn
So in my example, there is a simple controller variable, bucketToList, for example:
public String bucketToList {get;set;}
Next, there is also an action method defined in the controller class:
public PageReference listBucket(){
...do your logic here....but the bucketToList property is already set....
}
Awesome - my output label is now displaying the name of the selected Label Template object. I still can't get the JavaScript alert to display the name, but this still helps a lot. Thanks again!
- Carolyn
Oh nice man. You helped me alot. with this nice example. Thanks for this.