+ Start a Discussion
CvrKCvrK 

How to Enable/Disable a commandbutton and display an outputPanel based on selecting a picklist value?

Hi ,

How to Enable or Disable a commandbutton based on selecting a picklist value,
I have requirement to enable/disable a command button based on a picklist value then i have an Outputpanel(a fieldset inside) that has to be displayed when the button is clicked..the button has to be enabled only twice.

I am breaking my head since the three days, sample code will be very highly appreciated.
I am posting the question here after a lot of Online references, but i hav not find any online reference or code that is close to my requirement.


Thanks in Advance
Ck  
srlawr uksrlawr uk
Hi there. Could you post your existing page code - it will be a lot easier to add to it knowing your structure/conventions than to just start writing up a whole visualforce page just to include a commandbutton and a picklist with completely made up values!
jyothsna reddy 5jyothsna reddy 5
Hi ,
Please try this  below sample code:
<apex:page standardController="Opportunity">
<apex:form >
    <apex:pageBlock id="pBlock" title="New Opportunity" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandButton value="Cancel" action="{!cancel}"/>
            <apex:commandButton value="Won Notes" disabled="{!NOT(opportunity.StageName='Closed Won')}"/>
            <apex:actionStatus id="rStatus" startText="Refreshing..."/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection columns="1">
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Stage"/>
                <apex:actionRegion >
                <apex:inputField value="{!opportunity.StageName}">
                    <apex:actionSupport event="onchange" reRender="pBlock" status="rStatus"/>
                </apex:inputField>
            </apex:actionRegion>
            </apex:pageBlockSectionItem>
            <apex:inputField value="{!opportunity.Probability}"/>
            <apex:inputField value="{!opportunity.Name}"/>
            <apex:inputField value="{!opportunity.CloseDate}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>
I hope it will help you.
If the answer helped you ,mark it as solved.

Regards,
Jyothsna D
CvrKCvrK
Hi Srlawr,

Thanks for your time,i would like to let you know what exactly i am doing before you look at the code,hoping it makes easier for you to help me,

Please see image for the form that i am trying to build,

I am building webforms,intially just a picklist field called Request_Type__c (which has 6 values/options),so when a user select a picklist value among those ,its assigned fieldset will be displayed and upon clicking submit button record will be created, i am successfull till here,

Now my problem is.....when a user selects a picklist value  I.e "Course Modification Rrequest" as Request_Type__c  a fieldset will be displayed which has 5 fields ,now a button or any type of option has to be available/enabled to the user along wi the fields,so when user clicks on it, two fields among them
Modification_Type__c(Picklist) and Description__c(Text)  along with the same commandbutton or any option has to be displayed in the bottom again(could be an outputpane or pageblock),this same process has to be repeated one more time, that mean user should be able to select 3 picklist options from the Modification_Type field,along with the Description__C for each picklist value.

If you look at the image, all i need is a button/option for that particula requet type and when the user clicks the button Modification_Type__c field and Description__c field should be displayed in the bottom along with the same option/button


 
 
<apex:selectOption itemLabel="Course Modification Request" itemValue="F_Course_Modification_Request"></apex:selectOption>


User-added image
<apex:outputPanel id="ModifyClassFsetId" rendered="{!ModifyClassFsetId}">
<Apex:pageblock mode="edit" >
 <Apex:pageblockSection columns="1" >      
 <Apex:Repeat value="{!$ObjectType.Case_form_Extn__c.FieldSets.F_Modification_Class}" var="f">
 <apex:inputfield value="{!ocaseformextn[f]}"/>
 </apex:repeat>     
 </Apex:pageblockSection>
 </Apex:Pageblock>
 </apex:outputPanel>


 <Apex:commandbutton value="Modify another Item" action="{!RenderPanel}"  />
 
Variable:

 Public boolean ModifyClassFsetId {get;set;}


public PageReference RenderPanel(){
if(ocaseformextn.TDR_Request_Type__c=='F_Class_Offering_Modification' || ocaseformextn.TDR_Request_Type__c=='F_Course_Modification_Request' ){
            
            ModifyClassFsetId = true;
            }
           else 
           {
               ModifyClassFsetId = false;
            } 
            return null;
   }

The above method is not at all working,when i add the condition,please let me know if you need to know anything further.


Thanks in Advance.





 
srlawr uksrlawr uk
that looks like some good code to me, a nice effort, well done.

off the top of my head, maybe try this:
 
<apex:outputPanel id="ModifyClassFsetId" rendered="{!ModifyClassFsetId}">
<Apex:pageblock mode="edit" >
 <Apex:pageblockSection columns="1" >      
 <Apex:Repeat value="{!$ObjectType.Case_form_Extn__c.FieldSets.F_Modification_Class}" var="f">
 <apex:inputfield value="{!ocaseformextn[f]}"/>
 </apex:repeat>     
 </Apex:pageblockSection>
 </Apex:Pageblock>
 </apex:outputPanel>


 <Apex:commandbutton value="Modify another Item" action="{!RenderPanel}" rerender="ModifyClassFsetId" />

adding the "reRender" attribute to the commandbutton means it won't reload the whole page - which I think might be spoiling the scope of your ModifyClassFsetId variable... it will just rerender the output panel, maintaining the effort of your controller.


 
CvrKCvrK
Hi Srlawr,
No luck yet,....I have written a sample code below which is close to my requirement,Could you please provide a Class/method  and edit the vf code  accordingly, if you have time for this...

1)  As i said earlier, a command button has to be displayed upon selecting 2nd and 3rd selectoptions from below
       
                
<apex:actionRegion >
 <apex:selectList label="Request Type" value="{!ocaseformextn.TDR_Request_Type__c}" size="1" multiselect="false">
 <apex:actionSupport event="onchange"  action="{!requestTypeOnChange}" reRender="fieldSet"/>
 <apex:selectOption itemLabel="Attestations" itemValue="Attestations"></apex:selectOption>
 <apex:selectOption itemLabel="Class Offering Modification" itemValue="F_Class_Offering_Modification"></apex:selectOption>
 <apex:selectOption itemLabel="Course Modification Request" itemValue="F_Course_Modification_Request"></apex:selectOption>
 </apex:selectList>
</apex:actionRegion>



2)Upon click of button ,below outputpanel1/pageblock1, which has Field1__c,Field2__C or the the fieldset(either of them) should be displayed along with the same button,
  and this process has to be repeated once again and display Outputpanel2/pagegeblock2 which has Field3__c,Field4__C.
Note: i have included both fieldset and fields in the pageblocks either of them is fine,
 
<apex:outputPanel id="Fset1">
 <Apex:pageblock title=Pgb1>
 <Apex:pageblockSection columns="1" > 
 <Apex:Repeat value="{!$ObjectType.Case_form_Extn__c.FieldSets.F_Modification_Class1}" var="f">
  <apex:inputfield value="{!ocaseformextn[f]}"/>
  </apex:repeat>
  <Apex:OutputLabel >Modification Type</Apex:OutputLabel>
  <apex:inputfield value="{!ocaseformextn.Modification_Type1__c}" />
  <Apex:OutputLabel >Description</Apex:OutputLabel>
  <apex:inputfield value="{!ocaseformextn.Description_1__c}" />
   </Apex:pageblockSection>
   </Apex:Pageblock>
   </apex:outputPanel>

 <apex:outputPanel id="Fset2">
 <Apex:pageblock title=Pgb1>
 <Apex:pageblockSection columns="1" > 
 <Apex:Repeat value="{!$ObjectType.Case_form_Extn__c.FieldSets.F_Modification_Class2}" var="f">
  <apex:inputfield value="{!ocaseformextn[f]}"/>
  </apex:repeat>
 <Apex:OutputLabel >Modification Type</Apex:OutputLabel>
 <apex:inputfield value="{!ocaseformextn.Modification_Type2__c}" />
 <Apex:OutputLabel >Description</Apex:OutputLabel>
 <apex:inputfield value="{!ocaseformextn.Description_2__c}" />
   </Apex:pageblockSection>
   </Apex:Pageblock>
   </apex:outputPanel>