+ Start a Discussion
Ankit SinghalAnkit Singhal 

Creating a dropdown on Visualforce Page with values as Users.

Hi Everyone,

 

I need to create a dropdown field on Visualforce page which should display the list of Users based on a query.

Then I need to use the chosen  value in my Controller.

 

How to achieve this??

Plz help its urgent 

 

Thanks,

Ankit

Chamil MadusankaChamil Madusanka

Hi Ankit,

 

Try following code

 

Visualforce page

<apex:page controller="UserController">
  
  <apex:form >
      <apex:selectList size="1">
          <apex:selectOptions value="{!UserList}"></apex:selectOptions>
      </apex:selectList>
  </apex:form>
</apex:page>

 

Controller

public without sharing class UserController 
{
    public List<User> UserTemp = new List<User>();
    public UserController()
    {
    
    }
    
    public List<SelectOption> UserList
    {
        get
        {
            UserTemp = [Select u.LastName, u.Id, u.FirstName, u.Email From User u];
            
            UserList = new List<SelectOption>();
            
            for(User temp : UserTemp)
            {
                UserList.add(new SelectOption(temp.Id, temp.LastName));
            }
            return UserList;
        }
        set;
    }
}

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

 

AshlekhAshlekh

Hi Ankit

 

I wish this code should be helpfull for  you. 

 

Your apex visual force Page 

 

<apex:page controller="userController">

  <apex:form>

       <apex:pageBlock>

         <apex:pageBlockSection>

                      <apex:OutputPanel >

                                 <apex:selectList value="{!selecteduserId}" size="1" multiselect="false"  >
                                          <apex:selectOptions value="{!ListOfUser}" />
                                 </apex:selectList>
                        </apex:OutputPanel>

          </apex:pageBlockSection>

      </apex:pageBlock>

</apex:form>

</apex:page>

 

 

Apex class code:-

public with sharing class userController

{

       //This variable hold the id of  user which is selected by enduser from picklist

        public String selecteduserId {set;get;}

      

      //This var hold List of user

      public List<SelectOption> getListOfUser()

    {

               List<User> Users = [select id ,Username,name from user] ;

               List<SelectOption> UserOptionList = new List<SelectOption>();

               UserOptionList .add(new SelectOption( ' ' ,'---Select---'));

               for(User u : Users )
               {
                          UserOptionList .add(new SelectOption(u.Id , u.Name));
               }

              retrun UserOptionList ;

    }

 

  public userController()

  {

                    selecteduserId ='';

  }

 

}

 

Thanks

 

riazriaz

i want to give input to sheduler through visulaforce page by dropdownlist, any one can calp me,

regards

Derek Davis 7Derek Davis 7
Thanks Ashlekh! I found your suggestion very helpful!