Hello All,

It's been a while since I started coding in C# so please bare with me. Here's the scenario:

I have a form that has textbox fields such as "Qty, Price, and Total Amount".

"Qty" needs to be multiplied to "Price" and the results shown in the "Total Amount" textbox.

I would like to do it in C# codebehind but using Javascript would suffice.

Aspx Code is as follows:

QTY Text Box:

<asp:TextBox runat="server" name="qty100" ID="qty100" MaxLength="7" Width="70px" style="text-align:right" onkeyup='total(this,100);'></asp:TextBox>

Price Text Box:

<asp:TextBox runat="server" name="price100" ID="price100" 
MaxLength="10" Width="55px" style='text-align:right' onkeyup='totalPrice(this,100);' onfocus='this.value="$"' >

Total Amount TextBox:

<asp:TextBox runat="server" name="amount100" ID="amount100" MaxLength="14" Width="120px" style='text-align:right' value='$0.00' onpropertychange='grandTotal();' ReadOnly="true">

Javascript Functions:

function total(qty,itemNo)
{
    if(isNaN(val=parseInt(qty.value)*parseFloat(document.forms[0].elements["price"+itemNo].value.substr(document.forms[0].elements["price"+itemNo].value.indexOf("$")+1))))
        document.forms[0].elements["amount"+itemNo].value="$0.00";
    else
        document.forms[0].elements["amount"+itemNo].value="$" + FormatTotal(val,0);
}

function totalPrice(price,itemNo)
{
    if(isNaN(val=parseInt(document.forms[0].elements["qty"+itemNo].value)*parseFloat(price.value.substr(price.value.indexOf("$")+1))))
        document.forms[0].elements["amount"+itemNo].value="$0.00";
    else
        document.forms[0].elements["amount"+itemNo].value="$" + FormatTotal(val,0);
}

function grandTotal()
{
    var gt=0;
    for(i=0;i<document.forms[0].elements.length-54;i+=7)
    {
        gt+=parseFloat(document.forms[0].elements[i+36].value.substr(1));
    }
    document.forms[0].elements["grndtotal"].value="$" + FormatTotal(gt,-1);
}

Issue with Javascript:

On my local I get the following error once I type anything in one of the text boxes....I think it's because it's blank/null before it fires...how to fix??

Line: 551
Error: 'document.forms.0.elements[...].value' is null or not an object

Also, Would really like to write it in C# code behind but I keep getting the following error:

System.FormatException: Input string was not in a correct format.

Code in C# file:

protected void price100_TextChanged(object sender, EventArgs e)
    {
        int a = int.Parse(qty100.Text);

        int b = int.Parse(price100.Text);

        int c = a * b;

        amount100.Text = c.ToString();
    }

Hope that makes sense and thank you very much!!

MG

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Your C# code looks fine, the error you are getting is saying that there is something in the textbox that is not part of an integer (spaces, letters, something). I'd put a break on the first Parse() line and take a look at what is in the qty100 and price100 objects.

Hi Momerath,

Thanks for the reply. I don't quite understand what you mean be "break". Isn't break part of a switch case??

Thanks again,

~MG

Momerath,

I believe I got it all sorted out now. Textboxes were not NULL!! Had a '$' in some of them. DUH!!.............

Here is the code used below:

HTML CODE:

 <asp:TextBox runat="server" name="price100" ID="price100" 
                MaxLength="10" Width="55px" style='text-align:right'
                OnTextChanged="price100_TextChanged" AutoPostBack="true">

C# Code Behind:

 protected void price100_TextChanged(object sender, EventArgs e)
    {
        decimal result;
        string dollar = "$";
        result = Convert.ToDecimal(qty100.Text) * Convert.ToDecimal(price100.Text);
        amount100.Text = dollar + result.ToString();
        itm3.Focus();
    }

Thanks again, I appreciate the help!!!

~MG

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.