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