+ Start a Discussion
velgoti rani 18velgoti rani 18 

i have to disable and enable <apex:commandbutton> named "next" in visual force page depending on selecting some checkbox

Gaurav Jain 7Gaurav Jain 7
Hi,

Please use below code:

Mark it as Best Answer, if it helps

VF Page:
<apex:page controller="Sample" >
<apex:form >
    <apex:pageBlock id="pg" >
        <apex:pageblockSection >
            <apex:pageblockSectionItem >Check it to view the button:</apex:pageblockSectionItem>
            <apex:pageblockSectionItem >
                <apex:inputCheckbox value="{!option}" >
                    <apex:actionsupport event="onclick" action="{!change}" reRender="pg"/>
                </apex:inputCheckbox>                
            </apex:pageblockSectionItem>            
        </apex:pageblockSection>
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Submit" rendered="{!bool}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>    
</apex:page>


Apex class: 
 
public class Sample 
{
    public Boolean option {get;set;}
    public Boolean bool {get;set;}
    
    public Sample()
    {
         bool = false;
    }
    public void change()
    {
        if(option == true)
        {
            bool = true;
        }
        else
        {
            bool = false;
        }
    }
}

 
pradeep kumar yadavpradeep kumar yadav
The Above code good but a little change in it... because render will hide the button but you ask to disable...
 
<apex:page controller="Sample" >
<apex:form >
    <apex:pageBlock id="pg" >
        <apex:pageblockSection >
            <apex:pageblockSectionItem >Check it to view the button:</apex:pageblockSectionItem>
            <apex:pageblockSectionItem >
                <apex:inputCheckbox value="{!option}" >
                    <apex:actionsupport event="onclick" action="{!change}" reRender="pg"/>
                </apex:inputCheckbox>                
            </apex:pageblockSectionItem>            
        </apex:pageblockSection>
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Submit" disabled ="{!NOT(bool)}"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>    
</apex:page>
public class Sample 
{
    public Boolean option {get;set;}
    public Boolean bool {get;set;}
    
    public Sample()
    {
         bool = false;
    }
    public void change()
    {
        if(option == true)
        {
            bool = true;
        }
        else
        {
            bool = false;
        }
    }
}