[SalesForce] Parameter is passed as null

I have a Vf page and Controller as below:

<apex:page controller="DropdownDemoController">
<apex:form >  
    <apex:outputLabel value="Select a Name : "/>
    <apex:selectList value="{!selectedAccName}" multiselect="false" size="1">
        <apex:selectOptions value="{!accNames}"/>
    </apex:selectList><br/><br/>
    <apex:outputlink target="_blank" value="apex/Page2?accName={!selectedAccName}">Send</apex:outputlink>   
</apex:form>

Controller:

    public class DropdownDemoController 
    {
        public List<SelectOption> accNames{get;set;}
        public String selectedAccName{get;set;}

        public DropdownDemoController()
        {
            accNames = new List<SelectOption>();
            for(Account acc : [SELECT Name FROM Account LIMIT 5])
            {
                accNames.add(new SelectOption(acc.Name, acc.Name));            
            }
        }

        public void submit()
        {
            System.debug('selectedItem is '+selectedAccName);

        }
}

The page displays Account Names in a picklist. User can select an Account Name from the picklist and on clicking the Send link, the selected Account name will be passed to Page2 as a parameter. But it seems the parameter is passed as null instead of actual selected acc name in this case, can you please let me know why and how to correct it.

Thanks.

Best Answer

Refresh the outputlink when the value selected is changed in the accountnames,

<apex:page controller="DropdownDemoController">
<apex:form >  
<apex:actionfunction name="refreshOpLink" rerender="oplink"/>
    <apex:outputLabel value="Select a Name : "/>
    <apex:selectList value="{!selectedAccName}" multiselect="false" size="1" onchange="refreshOpLink();">
        <apex:selectOptions value="{!accNames}"/>
    </apex:selectList><br/><br/>
    <apex:outputlink id="oplink" target="_blank" value="apex/Page2?accName={!selectedAccName}">Send</apex:outputlink>   
</apex:form>

In the controller just edit the constructor to this to have the value on the selectedaccname on load

 public DropdownDemoController()
            {
                accNames = new List<SelectOption>();
                for(Account acc : [SELECT Name FROM Account LIMIT 5])
                {
                   if(selectedAccName==null)
                   {
                      selectedAccName=acc.name;
                   }
               accNames.add(new SelectOption(acc.Name,acc.Name));            
                }
            }
Related Topic