Stupid problem with Javascript calculations and postbacks

Posted by rockinthesixstring on Stack Overflow See other posts from Stack Overflow or by rockinthesixstring
Published on 2010-03-22T18:36:21Z Indexed on 2010/03/22 19:31 UTC
Read the original article Hit count: 366

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 %>');
    txtBalanceSheet = document.getElementById('<%=txtBalanceSheet.ClientID %>');
    txtDownPayment = document.getElementById('<%=txtDownPayment.ClientID %>');
    txtSusEarn = document.getElementById('<%=txtSusEarn.ClientID %>');
    txtSusRev = document.getElementById('<%=txtSusRev.ClientID %>');
    txtBalanceMult = document.getElementById('<%=txtBalanceMult.ClientID %>');
    txtGoodwillMult = document.getElementById('<%=txtGoodwillMult.ClientID %>');
    txtSellingPriceMult = document.getElementById('<%=txtSellingPriceMult.ClientID %>');
    txtGoodWill = document.getElementById('<%=txtGoodWill.ClientID %>');
    txtBalance = document.getElementById('<%=txtBalance.ClientID %>');
    chkTakeBack = document.getElementById('<%=chkTakeBack.ClientID %>');
    txtVendorTakeBackPercentage = document.getElementById('<%=txtVendorTakeBackPercentage.ClientID %>');
    txtSusEarnPercentage = document.getElementById('<%=txtSusEarnPercentage.ClientID %>');
    txtBalanceMultPercentage = document.getElementById('<%=txtBalanceMultPercentage.ClientID %>');
    txtGoodwillMultPercentage = document.getElementById('<%=txtGoodwillMultPercentage.ClientID %>');
    txtSellingPriceMultPercentage = document.getElementById('<%=txtSellingPriceMultPercentage.ClientID %>');

    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.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about JavaScript