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
Abhilasha_SinghAbhilasha_Singh 

Picklist value in VF Page

Hi,
I have to make one visualforce page in which one picklist field is there. If I select first value in picklist field one page will be open and if I select second value in picklist field then other page will open. 
How do i get this.. ?

Thanks
Abhilasha
hitesh90hitesh90
Hello Abhilasha,

Try to use following sample code for your requirement.

Visualforce Page:
<apex:page controller="picklistRedirectController">
    <apex:form>
        <apex:selectList value="{!strSelVFPage}" size="0" >
            <apex:selectOptions value="{!VFpages}"/>
            <apex:actionSupport event="onchange" action="{!redirectToPage}"/>
        </apex:selectList>
    </apex:form>
</apex:page>

Apex Class:
public class picklistRedirectController{
    public string strSelVFPage {get; set;}
    public List<SelectOption> getVFpages() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','None'));
        options.add(new SelectOption('VFPage1','VFPage1'));
        options.add(new SelectOption('VFPage2','VFPage2'));
        return options;
    }
    public pagereference redirectToPage(){
        if(strSelVFPage != ''){
            pagereference pr = new pagereference('/apex/' + strSelVFPage);
            return pr;
        }else{
            return null;
        }
    }  
}
Let me know if you have any question on this.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
sfdotcomsfdotcom
Hi Hitesh,
How to open page in new tab instead of same page ? I tried with "target=__blank" but it's not working..
hitesh90hitesh90
try this :- target="_blank"
sfdotcomsfdotcom
Hi,
Where to write target="_blank" in above page ?
hitesh90hitesh90
Tye to use following visualforce page. using JavaScript we can do this.

Visualforce Page:
<apex:page controller="picklistRedirectController">
    <script>
        function goToVFPage(pg){
            window.open("/apex/" + pg.value);
        }
    </script>
    <apex:form >
        <apex:selectList onchange="goToVFPage(this);" value="{!strSelVFPage}" size="0" >
            <apex:selectOptions value="{!VFpages}"/>            
        </apex:selectList>
    </apex:form>
</apex:page>

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
 
sfdotcomsfdotcom
Thank You Hitesh
mahish mahimahish mahi
getting error as "Error: picklistRedirectController Compile Error: Constructor not defined: [PageReference].<Constructor>(String) at line 14 column 32"