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
rajasfdcrajasfdc 

how to create and put dropdownlist in visualforce page?

how to create and put dropdownlist in visualforce page?i mean to say take an example country.no.of countries is there.how to take it as a dropdown or picklist in visaul force page.is there any need to create a custom controller.if it is need.pls clarify this how to create apex class for this purpose in visual force page.pls clarify i will wait.

thanks in advance.

stcforcestcforce

an input field referencing a picklist will present a dropdown box, won't it? For more dynamic population, however, you might need a controller (sometimes dynamic apex). What data features in the dropdown box is important. If the population of the dropdown box is fairly dynamic, then you will probably need a controller.

Rajesh SriramuluRajesh Sriramulu

Hi

 

Add this code :

 

<apex:selectList multiselect="false" size="1" value="{!object.Country}" required="false" >
   <apex:selectOption itemLabel="India" itemValue=""/>
   <apex:selectOption itemLabel="USA" itemValue=""/>
            
            </apex:selectList>

 

Hope this will helps u.

 

Regards,

Rajesh.

tukmoltukmol

You can write a controller for your VF page, and expose a property that provides an array of SelectOption object.

 

much like this:

 

the VF page

<apex:page controller="TesterController">
	<apex:form >
		<span>Countries: </span>
		<apex:selectList size="1">
			<apex:selectOptions value="{! countryOptions}" />		
		</apex:selectList>
	</apex:form>
</apex:page>

 the controller

public with sharing class TesterController {
	public TesterController() {
		String[] countries = new String[]{'Philippines','USA','India','Germany','Other countries...' };
		this.countryOptions = new SelectOption[]{};
		
		for (String c: countries) {
			this.countryOptions.add(new SelectOption(c,c));
		}
	}
	public SelectOption[] countryOptions { //this is where we're going to pull the list
		public get;
		private set;
	}
}

 

puneet-mishrapuneet-mishra

What if i dont want to hardcore the values 
like if i have to create drop down for years from 2000 to 2050 and i don't want to hardcore the years

David Smith 980David Smith 980
FYI for those finding this page, you may need to add "void" to the front of the 2nd line of the controller.

public void TesterController

I didn't and salesforce was throwing an "Invalid Constructor Name" compile error.