You need to sign in to do that
Don't have an account?
Dippan Patel 3
Unable to pass selected picklist value from Visualforce page to the controller.
Hi All,
I want to pass the picklist value selected from the visualforce page to my controller but I am getting a null value.
I want to pass the picklist value selected from the visualforce page to my controller but I am getting a null value.
<apex:page controller="PicklistController"> <apex:form > Enter Text: <apex:inputText id="txt1" value="{!textDemo}" /> <apex:selectList id="selectedUser" value="{!selectedUser}" size="1" onchange="setSelectedUser()"> <apex:selectOptions value="{!ActiveUsers}"></apex:selectOptions> </apex:selectList> </apex:form> </apex:page> public with sharing class PicklistController { public String selectedUser {get; set;} public String textDemo {get; set;} public List<SelectOption> getActiveUsers() { List<SelectOption> users = new List<SelectOption>(); List<User> allUsers = [select name from User where isactive = true]; system.debug(logginglevel.error, +allUsers); for(User u : allUsers){ String u1 = String.valueOf(u.name); users.add(new SelectOption('',u1)); } return users; } public void setSelectedUser(){ String test = selectedUser; system.debug(loggingLevel.error, 'selected user is:' + test); } }
Jayant explained well but you also need to modify your apex code users.add(new SelectOption('',u1));
You are using blank value in first parameter which always return selected value as null, it should be users.add(new SelectOption u1,u1));
Bellow is your modified code which will work.
Changes in your code:
1. In Visualforce page, ActionSupport and one outputPanel added
2. in Apex class, method getActiveUsers modified.
All Answers
To get the selected value, associate a apex:actionSupport to the apex:selectList to invoke the controller method. Something as:
Jayant explained well but you also need to modify your apex code users.add(new SelectOption('',u1));
You are using blank value in first parameter which always return selected value as null, it should be users.add(new SelectOption u1,u1));
Bellow is your modified code which will work.
Changes in your code:
1. In Visualforce page, ActionSupport and one outputPanel added
2. in Apex class, method getActiveUsers modified.
Thank you for your reply. Here is my updated code but I am getting the same null value.
Thank you. It worked!