Activity Forums Salesforce® Discussions how to set Button enable and disable mode

  • how to set Button enable and disable mode

    Posted by Uday on August 27, 2017 at 5:02 pm

    On vf page i have one submit button(default disable mode) and i have one picklist (value is hyd,che,bng),if any picklist value selected (submit button enable mode).Any one send me related code

    saloni gupta replied 7 years, 2 months ago 3 Members · 4 Replies
  • 4 Replies
  • Shubham

    Member
    August 29, 2017 at 8:03 am

    Hello Uday

    ****Controller****

    public class sampleCon {
    public String selectedImage{get;set;}

    public sampleCon(){
    selectedImage = '';
    }
    public PageReference test() {
    return null;
    }

    public List<SelectOption> getItems() {
    List<SelectOption> Images = new List<SelectOption>();
    Images.add(new SelectOption('hyd','hyd'));
    Images.add(new SelectOption('che','che'));
    Images.add(new SelectOption('bng','bng'));
    return Images;
    }

    public String getImage() {
    return selectedImage;
    }

    public void setImage(String selectedImage) {
    this.selectedImage = selectedImage;
    }
    }

    ****VisualForcePage****

    <apex:page controller="sampleCon">
    <apex:form>
    <apex:selectList value="{!selectedImage}" multiselect="true">
    <apex:selectOptions value="{!Items}"/>
    </apex:selectList><p/>
    <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
    </apex:form>
    <apex:outputPanel id="out">
    <apex:actionstatus id="status" startText="testing...">
    <apex:facet name="stop">
    <apex:form>
    <apex:commandButton value="Submit" disabled="{!If(selectedImage == '',True,False)}"/>
    </apex:form>
    </apex:facet>
    </apex:actionstatus>
    </apex:outputPanel>

    </apex:page>

  • Uday

    Member
    August 29, 2017 at 10:19 am

    thanks  #Shubham

    But i didn't understand below controler logic pls explain

    public String getImage() {
    return selectedImage;
    }

    public void setImage(String selectedImage) {
    this.selectedImage = selectedImage;

    vf

    disabled=”{!If(selectedImage == ”,True,False)}”

  • Shubham

    Member
    August 29, 2017 at 11:21 am

    Hello Uday

    ****In Controller****

    This is our getter and setter method(getImage and setImage) ,Here we First get value of our Image. Then in setter method we set this value to our declared variable(String selectedImage).

    ****InVF Page****

    There is an attribute disabled in "button" through this we enable or disable our button according o given condition.Condition is defined in If Block inside the disabled attribute.

    If selectedImage is empty then button is disable,Otherwise it is enable.

  • saloni gupta

    Member
    August 29, 2017 at 1:07 pm

    Hi Uday,

    Use the folllowing code.

    VF page:

    <apex:page controller="buttonEnableCont">
    <apex:form >
    <apex:pageBlock id="fid">
    <apex:selectList value="{!selectop}" multiselect="false" size="1" >
    <apex:selectOptions value="{!select}" id="sid" >
    </apex:selectOptions>
    <apex:actionSupport event="onchange" action="{!checkValue}" />
    </apex:selectList>
    <apex:commandButton value="submit" disabled="{!if(sub!=false,false,true)}" onclick="alert({!sub})" action ="{!save}" immediate="true" />
    </apex:pageBlock>
    </apex:form>
    </apex:page>

     

    apex class;

    public class buttonEnableCont {

    public string selectop{get;set;}
    public boolean sub{get;set;}

    public buttonEnableCont(){
    if(selectop=='hyd'||selectop=='che'||selectop=='bng'){
    sub= true;
    }else
    sub=false;

    }
    public List<SelectOption> getselect(){
    List<SelectOption> so = new List<SelectOption>();
    so.add(new SelectOption('-','none'));
    so.add(new SelectOption('hyd','hyd'));
    so.add(new SelectOption('che','che'));
    so.add(new SelectOption('bng','bng'));

    return so;

    }
    public void save(){
    system.debug('#########');
    }
    public void checkValue(){
    if(selectop=='hyd'||selectop=='che'||selectop=='bng'){
    sub= true;
    }else
    sub=false;
    }

    }

Log In to reply.

Popular Salesforce Blogs