+ Start a Discussion
communitycommunity 

Display command button when any picklist value is selected.

Hi,

 

Im struggling with the actionSupport function in visualforce.

 

If the picklist value of 'Address_Type__c' is other than None , then the command button should be displayed.

 

Visualforce page:

<apex:page standardController="Address_Details__c" extensions="CRFNewAddressSelect">
<apex:form >

<apex:sectionHeader Title="Address Details" />
    <apex:pageBlock Title="Select the type of Address">
    
        <apex:pageBlockSection columns="1">
            <apex:PageBlockSectionItem >
                    <apex:outputLabel value="{!$ObjectType.Address_Details__c.fields.Address_Type__c.label}"/>
                    
                    <apex:selectList id="id_1" value="{!Address_Details__c.Address_Type__c}" size="1" >
                    <apex:actionSupport event="onchange" reRender="ajaxrequest" immediate="True"/>
                    <apex:selectOptions value="{!items}"/>
                     
                    </apex:selectList>

            </apex:PageBlockSectionItem>

            <apex:PageBlockSectionItem >
             
            <apex:outputPanel id="ajaxrequest" rendered="{!disp}">
                <apex:commandButton value="Continue"/>
            </apex:outputPanel>
            
            </apex:PageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

 

 

Controller:

 

public with sharing class CRFNewAddressSelect {

private Address_Details__c AD;
    public CRFNewAddressSelect(ApexPages.StandardController stdcontroller) {
    this.AD=(Address_Details__c)stdController.getRecord();
    }

Boolean disp=False;

Public Boolean getdisp(){
return disp;
}
    
String Values;

public String getValues() {
            return Values;
        }
            
        public void setValues(String Values) {
            this.Values= Values;
            if(Values!=null)
            disp=True;
        }
public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('None','None'));
            options.add(new SelectOption('CA','Cease Address'));
            options.add(new SelectOption('CANADA','Canada'));
            options.add(new SelectOption('MEXICO','Mexico'));
            return options;
        }

}

 

I tried everything possible. Its not working.  For text fields my code does work, but for picklist, I think I am missing something. Help

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Ah yes - this section:

 

 

String Values;

public String getValues() {
            return Values;
        }
            
        public void setValues(String Values) {
            this.Values= Values;
            if(Values!=null)
            disp=True;
        }

 

 

Doesn't appear to be tied into the page, so disp is never being updated to true.

 

You should be able to handle this without recourse to the controller.  Simply render your button based on the selected value, which will be stored in Address_Details__c.Address_Type__c,  e.g.

 

 

<apex:sectionHeader Title="Address Details" />
    <apex:pageBlock Title="Select the type of Address">
    
        <apex:pageBlockSection columns="1">
            <apex:PageBlockSectionItem >
                    <apex:outputLabel value="{!$ObjectType.Address_Details__c.fields.Address_Type__c.label}"/>
                    
                    <apex:selectList id="id_1" value="{!Address_Details__c.Address_Type__c}" size="1" >
                    <apex:actionSupport event="onchange" reRender="Container"/>
                    <apex:selectOptions value="{!items}"/>
                     
                    </apex:selectList>

            </apex:PageBlockSectionItem>

            <apex:PageBlockSectionItem >
            <apex:outputPanel id="Container">
            <apex:outputPanel id="ajaxrequest" rendered="{!Address_Details__c.Address_Type__c!='None'}">
                <apex:commandButton value="Continue"/>
            </apex:outputPanel>
            </apex:outputPanel>
            </apex:PageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>

 

 

 

 

All Answers

bob_buzzardbob_buzzard

You have specified immediate="True" on your actionsupport - this means that your selected value will be discarded rather than propagated back to the controller.

 

You may also hit problems with rerendering the ajaxrequest outputpanel if it wasn't present when the page was originally drawn - I've written a blog post on this at:

 

http://bobbuzzard.blogspot.com/2011/02/visualforce-re-rendering-woes.html

communitycommunity

Thanks for the reply.   I added the output panel container to the existing one and removed immediate = true from the actionSupport, but still it doesn't work.

 

 

<apex:sectionHeader Title="Address Details" />
    <apex:pageBlock Title="Select the type of Address">
    
        <apex:pageBlockSection columns="1">
            <apex:PageBlockSectionItem >
                    <apex:outputLabel value="{!$ObjectType.Address_Details__c.fields.Address_Type__c.label}"/>
                    
                    <apex:selectList id="id_1" value="{!Address_Details__c.Address_Type__c}" size="1" >
                    <apex:actionSupport event="onchange" reRender="Container"/>
                    <apex:selectOptions value="{!items}"/>
                     
                    </apex:selectList>

            </apex:PageBlockSectionItem>

            <apex:PageBlockSectionItem >
            <apex:outputPanel id="Container">
            <apex:outputPanel id="ajaxrequest" rendered="{!disp}">
                <apex:commandButton value="Continue"/>
            </apex:outputPanel>
            </apex:outputPanel>
            </apex:PageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>

 

 

Is there anything going wrong in the controller ?

 

Thanks!

bob_buzzardbob_buzzard

Ah yes - this section:

 

 

String Values;

public String getValues() {
            return Values;
        }
            
        public void setValues(String Values) {
            this.Values= Values;
            if(Values!=null)
            disp=True;
        }

 

 

Doesn't appear to be tied into the page, so disp is never being updated to true.

 

You should be able to handle this without recourse to the controller.  Simply render your button based on the selected value, which will be stored in Address_Details__c.Address_Type__c,  e.g.

 

 

<apex:sectionHeader Title="Address Details" />
    <apex:pageBlock Title="Select the type of Address">
    
        <apex:pageBlockSection columns="1">
            <apex:PageBlockSectionItem >
                    <apex:outputLabel value="{!$ObjectType.Address_Details__c.fields.Address_Type__c.label}"/>
                    
                    <apex:selectList id="id_1" value="{!Address_Details__c.Address_Type__c}" size="1" >
                    <apex:actionSupport event="onchange" reRender="Container"/>
                    <apex:selectOptions value="{!items}"/>
                     
                    </apex:selectList>

            </apex:PageBlockSectionItem>

            <apex:PageBlockSectionItem >
            <apex:outputPanel id="Container">
            <apex:outputPanel id="ajaxrequest" rendered="{!Address_Details__c.Address_Type__c!='None'}">
                <apex:commandButton value="Continue"/>
            </apex:outputPanel>
            </apex:outputPanel>
            </apex:PageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>

 

 

 

 

This was selected as the best answer
communitycommunity

Thanks a ton mate!It worked awesome!!!! :)

 

Infact it was 

rendered="{!Address_Details__c.Address_Type__c!=''}">

for null values.

 

Cheers!