I'm working on an ASP.NET web app where I'm using a Wizard to take the client through a large series of steps.
One of the steps includes calculating a bunch of numbers on the fly... the numbers calculate properly but when I click "next" and then go back again... some of the numbers are not retained.
Here is the calculation function
function CalculateFields() {
txtSellingPrice = document.getElementById('<%=txtSellingPrice.ClientID %>');
//I get all of the elements like this...
//...
var regexp = /[$,]/g;
//Empty value checks
SellingPrice = (SellingPrice == "" ? "$0" : SellingPrice);
BalanceSheet = (BalanceSheet == "" ? "$0" : BalanceSheet);
DownPayment = (DownPayment == "" ? "$0" : DownPayment);
susearn = (susearn == "" ? "$0" : susearn);
susrev = (susrev == "" ? "$0" : susrev);
balmult = (balmult == "" ? "$0" : balmult);
goodmult = (goodmult == "" ? "$0" : goodmult);
sellmult = (sellmult == "" ? "$0" : sellmult);
//Replace $ with String.Empty
SellingPrice = txtSellingPrice.value.replace(regexp, "");
BalanceSheet = txtBalanceSheet.value.replace(regexp, "");
DownPayment = txtDownPayment.value.replace(regexp, "");
susearn = txtSusEarn.value.replace(regexp, "");
susrev = txtSusRev.value.replace(regexp, "");
balmult = txtBalanceMult.value.replace(regexp, "");
goodmult = txtGoodwillMult.value.replace(regexp, "");
sellmult = txtSellingPriceMult.value.replace(regexp, "");
//Set the new values
txtGoodWill.value = "$" + (SellingPrice - BalanceSheet);
txtBalance.value = "$" + (SellingPrice - DownPayment);
txtSellingPriceMult.value = "$" + SellingPrice;
txtGoodwillMult.value = "$" + (SellingPrice - BalanceSheet);
txtBalanceMult.value = "$" + BalanceSheet;
if (chkTakeBack.checked == 1) {
txtVendorTakeBackPercentage.value = Math.round((SellingPrice - DownPayment) / SellingPrice * 100);
}
else {
txtVendorTakeBackPercentage.value = "0";
}
if (!(susearn == "") && !(susearn == "0") && !(susearn == "$0")) {
txtSusEarnPercentage.value = Math.round(susearn / susrev * 100);
txtBalanceMultPercentage.value = Math.round(balmult / susearn);
txtGoodwillMultPercentage.value = Math.round(goodmult / susearn);
txtSellingPriceMultPercentage.value = Math.round(sellmult / susearn);
}
else {
txtSusEarnPercentage.value = "0";
txtBalanceMultPercentage.value = "0";
txtGoodwillMultPercentage.value = "0";
txtSellingPriceMultPercentage.value = "0";
}
}
all of these calculate properly and retain their value across postbacks
txtGoodWill.value = "$" + (SellingPrice - BalanceSheet);
txtBalance.value = "$" + (SellingPrice - DownPayment);
txtSellingPriceMult.value = "$" + SellingPrice;
txtGoodwillMult.value = "$" + (SellingPrice - BalanceSheet);
txtBalanceMult.value = "$" + BalanceSheet;
These ones however do not retain their value across postbacks
if (chkTakeBack.checked == 1) {
txtVendorTakeBackPercentage.value = Math.round((SellingPrice - DownPayment) / SellingPrice * 100);
}
else {
txtVendorTakeBackPercentage.value = "0";
}
if (!(susearn == "") && !(susearn == "0") && !(susearn == "$0")) {
txtSusEarnPercentage.value = Math.round(susearn / susrev * 100);
txtBalanceMultPercentage.value = Math.round(balmult / susearn);
txtGoodwillMultPercentage.value = Math.round(goodmult / susearn);
txtSellingPriceMultPercentage.value = Math.round(sellmult / susearn);
}
else {
txtSusEarnPercentage.value = "0";
txtBalanceMultPercentage.value = "0";
txtGoodwillMultPercentage.value = "0";
txtSellingPriceMultPercentage.value = "0";
}
The txtVendorTakeBackPercentage
always comes back BLANK
and the other three always come back as 0
I'm firing these functions by using the onkeyup
event within the form fields.
If Not Page.IsPostBack Then
txtSellingPrice.Attributes.Add("onkeyup", "CalculateFields()")
txtBalanceSheet.Attributes.Add("onkeyup", "CalculateFields()")
txtDownPayment.Attributes.Add("onkeyup", "CalculateFields()")
txtSusRev.Attributes.Add("onkeyup", "CalculateFields()")
txtSusEarn.Attributes.Add("onkeyup", "CalculateFields()")
End If
any thoughts/help/direction would be greatly appreciated.
EDIT:
My page load event looks something like this...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'Prepair page for advanced validation'
Dim cs As ClientScriptManager = Page.ClientScript
If Not cs.IsOnSubmitStatementRegistered(Me.[GetType](), "PrepareValidationSummary") Then _
cs.RegisterOnSubmitStatement(Me.[GetType](), "PrepareValidationSummary", "CheckForm()")
If Not Page.IsPostBack Then
'Part of the client side math'
txtSellingPrice.Attributes.Add("onkeyup", "CalculateFields()")
txtBalanceSheet.Attributes.Add("onkeyup", "CalculateFields()")
txtDownPayment.Attributes.Add("onkeyup", "CalculateFields()")
txtSusRev.Attributes.Add("onkeyup", "CalculateFields()")
txtSusEarn.Attributes.Add("onkeyup", "CalculateFields()")
My actual aspx page has got over 2400 lines, so I'm not going to post the whole thing. Here is a section where the percentage fields are not retaining their values.
<tr>
<td colspan="5" class="AlignRight">
Average Annual Sustainable Earnings (ebitda) estimated
<ew:NumericBox ID="txtSusEarnPercentage" runat="server" Enabled="False" MaxLength="3"
Width="20px" TruncateLeadingZeros="True" />
%
</td>
<td class="AlignRight">
<ew:NumericBox ID="txtSusEarn" runat="server" AutoFormatCurrency="True" DecimalPlaces="2"
MaxLength="9" Width="92px" />
</td>
<td>
<asp:Image ID="HelpEBITDA0" runat="server" Height="16px" ImageUrl="~/Images/Icons/help.gif" />
</td>
</tr>
<tr>
<td colspan="5" class="AlignRight">
</td>
<td class="AlignRight">
</td>
<td>
</td>
</tr>
<tr>
<td colspan="5" class="AlignRight">
Balance Sheet / Net Asset Value as earnings multiple
<ew:NumericBox ID="txtBalanceMultPercentage" runat="server" Enabled="False" MaxLength="3"
Width="20px" />
</td>
<td class="AlignRight">
<ew:NumericBox ID="txtBalanceMult" runat="server" AutoFormatCurrency="True" DecimalPlaces="2"
MaxLength="9" Width="92px" Enabled="False" />
</td>
<td>
</td>
</tr>
<tr>
<td colspan="5" class="AlignRight">
Goodwill Value as earnings multiple
<ew:NumericBox ID="txtGoodwillMultPercentage" runat="server" Enabled="False" MaxLength="3"
Width="20px" />
</td>
<td class="AlignRight">
<ew:NumericBox ID="txtGoodwillMult" runat="server" AutoFormatCurrency="True" DecimalPlaces="2"
MaxLength="9" Width="92px" Enabled="False" />
</td>
<td>
</td>
</tr>
<tr>
<td colspan="5" class="AlignRight">
Proposed Selling Price as earnings multiple
<ew:NumericBox ID="txtSellingPriceMultPercentage" runat="server" Enabled="False"
MaxLength="3" Width="20px" />
</td>
<td class="AlignRight">
<ew:NumericBox ID="txtSellingPriceMult" runat="server" AutoFormatCurrency="True"
DecimalPlaces="2" MaxLength="9" Width="92px" Enabled="False" />
</td>
<td>
</td>
</tr>
Here is the form when I fill it in
And here is the form after I hit "next" and then "previous"