You need to sign in to do that
Don't have an account?
DannyK89
Prevent Page Block Button from Going Off
Hi everyone,
So, I was working on a visualforce page. I would like to know if there is a way to prevent the first Page Block Button from running when you press the enter key. I would like the enter key to run another function in my controller. If you need anything more from me please let me know.
** Visualforce code: <apex:page controller="CalcController"> <apex:form id="form"> <apex:pageBlock title="Margin Calculator" id="page"> <apex:pageBlockButtons > <apex:commandButton value="Back" action="/home/home.jsp"/> <apex:commandButton value="Calculate" id="test"/> </apex:pageBlockButtons> <apex:messages style="font-size:16px; color:red;" id="error"/> <apex:pageBlockSection columns="1" id="Main"> <!--<apex:inputField />--> <apex:selectList value="{!Type}" size="1" label="Pay Status"> <apex:selectOption itemLabel="W2-Hourly" itemValue="W2-Hourly"/> <apex:selectOption itemLabel="Corp-Empl" itemValue="Corp-Empl"/> <apex:selectOption itemLabel="Corp-Prin" itemValue="Corp-Prin"/> <apex:selectOption itemLabel="SS-1099" itemValue="SS-1099"/> <apex:selectOption itemLabel="Salary" itemValue="Salary"/> <apex:actionSupport event="onchange" action="{!Calculate}" reRender="page"/> </apex:selectList> <apex:inputText label="Pay Rate" value="{!PayRate}" onkeypress="return noenter();"> </apex:inputText> <apex:inputText label="Bill Rate" value="{!BillRate}"> <apex:actionSupport event="onchange" action="{!Calculate}" reRender="page"/> </apex:inputText> <apex:inputText label="Discount Percent" value="{!DiscountPercent}"> <apex:actionSupport event="onchange" action="{!Calculate}" reRender="page"/> </apex:inputText> <apex:pageBlockSectionItem > <apex:outputLabel /> <apex:commandButton value="Clear" action="{!Clear}"/> </apex:pageBlockSectionItem> <!--<apex:inputText label="Gross Margin" value="{!GrossMargin}"/> <apex:inputText label="Gross Margin Pct" value="{!GrossMarginPct}"/>--> <apex:outputText label="Gross Margin" value="${!GrossMargin}"/> <apex:outputText label="Gross Margin Pct" value="{!GrossMarginPct}%"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page> ** Controller Code: public class CalcController { public String Type { get; set; } public Decimal BillRate { get; set; } public Decimal PayRate { get; set; } public Decimal DiscountPercent { get; set; } public Decimal GrossMargin { get; set; } public Decimal GrossMarginPct { get; set; } public CalcController(){ } public void Calculate(){ if(DiscountPercent == null) DiscountPercent = 0; if(Type=='Corp-Empl' || Type=='Corp-Prin' || Type=='SS-1099'){ if(BillRate != null && PayRate != null && BillRate != 0 && PayRate != 0){ GrossMargin = BillRate - PayRate - (PayRate*0.03) - (BillRate*(DiscountPercent/100)); GrossMarginPct = 100*(GrossMargin / (BillRate - (BillRate * (DiscountPercent/100)))); GrossMargin = GrossMargin.setScale(2, System.RoundingMode.HALF_UP); GrossMarginPct = GrossMarginPct.setScale(2, System.RoundingMode.HALF_UP); } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'ERROR: Please enter the Bill Rate and Pay Rate'); ApexPages.addMessage(myMsg); } /*if(GrossMargin != null && GrossmarginPct != null && GrossMargin != 0 && GrossMarginPct != 0){ BillRate = (GrossMargin/(GrossMarginPct/100)); BillRate += (BillRate*(DiscountPercent/100)); PayRate = ((GrossMargin - BillRate + (BillRate*(DiscountPercent/100)))/-1.03); BillRate = BillRate.setScale(2, System.RoundingMode.HALF_UP); PayRate = PayRate.setScale(2, System.RoundingMode.HALF_UP); } else if(BillRate != null && PayRate != null && BillRate != 0 && PayRate != 0){ GrossMargin = BillRate - PayRate - (PayRate*0.03) - (BillRate*(DiscountPercent/100)); GrossMarginPct = 100*(GrossMargin / (BillRate - (BillRate * (DiscountPercent/100)))); } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'ERROR: Please enter the Bill Rate and Pay Rate or Gross Margin and Gross Margin Pct'); ApexPages.addMessage(myMsg); }*/ } else if(Type=='Salary'){ if(BillRate != null && PayRate != null && BillRate != 0 && PayRate != 0){ GrossMargin = BillRate - PayRate - (PayRate*0.25) - (BillRate*(DiscountPercent/100)); GrossMarginPct = 100*(GrossMargin / (BillRate - (BillRate * (DiscountPercent/100)))); GrossMargin = GrossMargin.setScale(2, System.RoundingMode.HALF_UP); GrossMarginPct = GrossMarginPct.setScale(2, System.RoundingMode.HALF_UP); } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'ERROR: Please enter the Bill Rate and Pay Rate'); ApexPages.addMessage(myMsg); } /*if(GrossMargin != null && GrossMarginPct != null && GrossMargin != 0 && GrossMarginPct != 0){ BillRate = (GrossMargin/(GrossMarginPct/100)); BillRate += (BillRate*(DiscountPercent/100)); PayRate = ((GrossMargin - BillRate + (BillRate*(DiscountPercent/100)))/-1.25); BillRate = BillRate.setScale(2, System.RoundingMode.HALF_UP); PayRate = PayRate.setScale(2, System.RoundingMode.HALF_UP); } else if(BillRate != null && PayRate != null && BillRate != 0 && PayRate != 0){ GrossMargin = BillRate - PayRate - (PayRate*0.25) - (BillRate*(DiscountPercent/100)); GrossMarginPct = 100*(GrossMargin / (BillRate - (BillRate * (DiscountPercent/100)))); } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'ERROR: Please enter the Bill Rate and Pay Rate or Gross Margin and Gross Margin Pct'); ApexPages.addMessage(myMsg); }*/ } else if(Type=='W2-Hourly'){ if(BillRate != null && PayRate != null && BillRate != 0 && PayRate != 0){ GrossMargin = BillRate - PayRate - (PayRate*0.15) - (BillRate*(DiscountPercent/100)); GrossMarginPct = 100*(GrossMargin / (BillRate - (BillRate * (DiscountPercent/100)))); GrossMargin = GrossMargin.setScale(2, System.RoundingMode.HALF_UP); GrossMarginPct = GrossMarginPct.setScale(2, System.RoundingMode.HALF_UP); } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'ERROR: Please enter the Bill Rate and Pay Rate'); ApexPages.addMessage(myMsg); } /*if(GrossMargin != null && GrossMarginPct != null && GrossMargin != 0 && GrossMarginPct != 0){ BillRate = (GrossMargin/(GrossMarginPct/100)); BillRate += (BillRate*(DiscountPercent/100)); PayRate = ((GrossMargin - BillRate + (BillRate*(DiscountPercent/100)))/-1.15); BillRate = BillRate.setScale(2, System.RoundingMode.HALF_UP); PayRate = PayRate.setScale(2, System.RoundingMode.HALF_UP); } else if(BillRate != null && PayRate != null && BillRate != 0 && PayRate != 0){ GrossMargin = BillRate - PayRate - (PayRate*0.15) - (BillRate*(DiscountPercent/100)); GrossMarginPct = 100*(GrossMargin / (BillRate - (BillRate * (DiscountPercent/100)))); } else{ ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'ERROR: Please enter the Bill Rate and Pay Rate or Gross Margin and Gross Margin Pct'); ApexPages.addMessage(myMsg); }*/ } } public void Clear(){ BillRate = PayRate = DiscountPercent = GrossMargin = GrossMarginPct = 0; } static testMethod void TestController(){ CalcController controller = new CalcController(); controller.Type ='Corp-Prin'; controller.PayRate = 35.00; controller.BillRate = 55.00; controller.DiscountPercent = 0.00; controller.Calculate(); controller.Type ='Corp-Prin'; controller.PayRate = 0.00; controller.BillRate = 0.00; controller.DiscountPercent = 0.00; controller.Calculate(); controller.Type ='Salary'; controller.PayRate = 35.00; controller.BillRate = 55.00; controller.DiscountPercent = 0.00; controller.Calculate(); controller.Type ='Salary'; controller.PayRate = 0.00; controller.BillRate = 0.00; controller.DiscountPercent = 0.00; controller.Calculate(); controller.Type ='W2-Hourly'; controller.PayRate = 35.00; controller.BillRate = 55.00; controller.DiscountPercent = 0.00; controller.Calculate(); controller.Type ='W2-Hourly'; controller.PayRate = 0.00; controller.BillRate = 0.00; controller.DiscountPercent = 0.00; controller.Calculate(); controller.Clear(); } }