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
Saie Shendage 7Saie Shendage 7 

How to display dropdown list on a VF page and how to retrieve its selected value?

User-added image
Note : I am referring Account object as a 'Society' and Contact object as its respective 'Members'. Secretory_Name__c is my custom field on Account object, secretory is nothing but one the contacts associated with the account.

In the 'Add Secretory' section of this page, I want to display dropdown list which shows me contact names associated with the respective account. Then I want to use that selected contact name to assign it to the secretory field.

Please someone help me with VF page and its custom controller. Thank you in advance.
Best Answer chosen by Saie Shendage 7
Malika Pathak 9Malika Pathak 9

Hi Saie Shendage,

Please find the solution.

<apex:page controller="DropdownMenu" >
    <apex:form >
        Dropdown Menu
        <apex:selectList required="true" multiselect="false" size="1" label="Type"  value="{!selectedValue}">
            <apex:selectOptions value="{!Items}"/>
        </apex:selectList>
    </apex:form>
</apex:page>




Controller:-

public class DropdownMenu {
    public string selectedValue{get;set;}
    public DropdownMenu()
    {
        selectedValue='Add Society';
    }
    public List<SelectOption> getItems()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--Select--','--Select--'));
        options.add(new SelectOption('Add Society','Add Society'));
        options.add(new SelectOption('Name','Name'));
        return options;
    }
}
 

If you find this solution is helpful for you then please mark best answer

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Saie,

>> https://developer.salesforce.com/forums/?id=906F000000098SBIAY

The above link has an example of using a dropdown in visualforce page along with a controller you can try checking this and modify it as per your use case.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Malika Pathak 9Malika Pathak 9

Hi Saie Shendage,

Please find the solution.

<apex:page controller="DropdownMenu" >
    <apex:form >
        Dropdown Menu
        <apex:selectList required="true" multiselect="false" size="1" label="Type"  value="{!selectedValue}">
            <apex:selectOptions value="{!Items}"/>
        </apex:selectList>
    </apex:form>
</apex:page>




Controller:-

public class DropdownMenu {
    public string selectedValue{get;set;}
    public DropdownMenu()
    {
        selectedValue='Add Society';
    }
    public List<SelectOption> getItems()
    {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('--Select--','--Select--'));
        options.add(new SelectOption('Add Society','Add Society'));
        options.add(new SelectOption('Name','Name'));
        return options;
    }
}
 

If you find this solution is helpful for you then please mark best answer

This was selected as the best answer