0

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.

  • rerender the outputlink to refresh the value of the selected account name using actionfunction or actionsupport – Preya Jan 18 '17 at 12:21
2

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));            
                }
            }
| improve this answer | |
  • Hi Preya, thanks for the response, your code almost worked. Only change to your code is, I kept apex:outputlink inside apex:outputPanel and it worked. But if user dont select the picklist and submits(with default populated value of picklist) it still goes as null. – Jaya Jan 18 '17 at 13:46
  • edited the answer to support onload. I hope you are not loading the page with already selected accountname? – Preya Jan 18 '17 at 13:56
0

You are passing selectedAccName as url parameter. So try to get

String selectedAccName = apexpages.currentpage().getparameters().get('accName');
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.