asaworker
asked on
ColdFusion cfloop to ASP.NET C# equivalent
I have an old legabcy ColdFsauion site that I am converting into mASP.NET C#. I have a cfloop inside my javascript code that works great. I have to convert that to ASP.NET. Can you do this inline to the page or does it need to be in the .cs file?
An example of the old way:
function CalculateRowTotal(num) {
var order_total = 0;
//make all fields for the order form 0 by default for total reasons
<cfloop from="1" to="15" index="i">
if(document.po_form.Extend ed_Cost_#i #.value =="" || document.po_form.Extended_ Cost_#i#.v alue ==null) {
document.po_form.Extended_ Cost_#i#.v alue=0;
}
if(document.po_form.Unit_C ost_#i#.va lue =="" || document.po_form.Unit_Cost _#i#.value ==null) {
document.po_form.Unit_Cost _#i#.value =0;
}
if(document.po_form.Quanti ty_#i#.val ue =="" || document.po_form.Quantity_ #i#.value ==null) {
document.po_form.Quantity_ #i#.value= 0;
}
if(document.po_form.Hidden _Extended_ Cost_#i#.v alue =="" || document.po_form.Hidden_Ex tended_Cos t_#i#.valu e ==null) {
document.po_form.Hidden_Ex tended_Cos t_#i#.valu e=0;
}
if(document.po_form.Insert ed_Extende d_Cost_#i# .value =="" || document.po_form.Inserted_ Extended_C ost_#i#.va lue ==null) {
document.po_form.Inserted_ Extended_C ost_#i#.va lue=0;
}
</cfloop>
//again
// Run through all the form fields
// Get the current field
var form_field1 = eval("document.po_form.Uni t_Cost_" + num);
//alert(form_field2);
var form_field2 = eval("document.po_form.Qua ntity_" + num);
var form_field3 = eval("document.po_form.Ext ended_Cost _" + num);
var form_field4 = eval("document.po_form.Hid den_Extend ed_Cost_" + num);
var form_field5 = eval("document.po_form.Ins erted_Exte nded_Cost_ " + num);
//Get the Unit Cost
var unit_cost = parseFloat(form_field1.val ue);
// Get the Quantity
var quantity = parseInt(form_field2.value );
//Get the Extended Cost
var extendedcost = parseFloat(form_field3.val ue);
// Update the Extended Cost, Quantity, or Unit Cost
if ((quantity >= 1 && quantity != "" && quantity !=null) && (unit_cost > 0 && unit_cost != "" && unit_cost !=null)) {
order_total += quantity * unit_cost;
form_field3.value = round_decimals(order_total , 2);
form_field4.value = form_field3.value;
form_field5.value = form_field3.value;
//var newValues = addCommas(form_field3.valu e);
//form_field3.value = newValues;
form_field1.value = round_decimals(unit_cost, 5);
//var newValue = addCommas(form_field5.valu e);
//form_field5.value = newValue;
} else if((quantity >= 1 && quantity != "" && quantity !=null) && (extendedcost > 0 && extendedcost != "" && extendedcost !=null)) {
order_total += extendedcost / quantity;
form_field1.value = round_decimals(order_total , 5);
form_field5.value = extendedcost;
form_field5.value = round_decimals(extendedcos t, 2);
form_field4.value = form_field5.value
//var newValues = addCommas(form_field5.valu e);
//form_field3.value = newValues;
//var newValue = addCommas(form_field5.valu e);
//form_field5.value = newValue;
} else if((unit_cost > 0 && unit_cost != "" && unit_cost !=null) && (extendedcost > 0 && extendedcost != "" && extendedcost !=null)) {
order_total += extendedcost / unit_cost;
form_field1.value = round_decimals(unit_cost, 5);
form_field2.value = order_total;
form_field3.value = extendedcost;
form_field3.value = round_decimals(extendedcos t, 2);
form_field5.value = form_field3.value
form_field4.value = form_field5.value
//var newValue = addCommas(form_field5.valu e);
//form_field3.value = newValue;
//form_field5.value = newValue;
}
}
An example of the old way:
function CalculateRowTotal(num) {
var order_total = 0;
//make all fields for the order form 0 by default for total reasons
<cfloop from="1" to="15" index="i">
if(document.po_form.Extend
document.po_form.Extended_
}
if(document.po_form.Unit_C
document.po_form.Unit_Cost
}
if(document.po_form.Quanti
document.po_form.Quantity_
}
if(document.po_form.Hidden
document.po_form.Hidden_Ex
}
if(document.po_form.Insert
document.po_form.Inserted_
}
</cfloop>
//again
// Run through all the form fields
// Get the current field
var form_field1 = eval("document.po_form.Uni
//alert(form_field2);
var form_field2 = eval("document.po_form.Qua
var form_field3 = eval("document.po_form.Ext
var form_field4 = eval("document.po_form.Hid
var form_field5 = eval("document.po_form.Ins
//Get the Unit Cost
var unit_cost = parseFloat(form_field1.val
// Get the Quantity
var quantity = parseInt(form_field2.value
//Get the Extended Cost
var extendedcost = parseFloat(form_field3.val
// Update the Extended Cost, Quantity, or Unit Cost
if ((quantity >= 1 && quantity != "" && quantity !=null) && (unit_cost > 0 && unit_cost != "" && unit_cost !=null)) {
order_total += quantity * unit_cost;
form_field3.value = round_decimals(order_total
form_field4.value = form_field3.value;
form_field5.value = form_field3.value;
//var newValues = addCommas(form_field3.valu
//form_field3.value = newValues;
form_field1.value = round_decimals(unit_cost, 5);
//var newValue = addCommas(form_field5.valu
//form_field5.value = newValue;
} else if((quantity >= 1 && quantity != "" && quantity !=null) && (extendedcost > 0 && extendedcost != "" && extendedcost !=null)) {
order_total += extendedcost / quantity;
form_field1.value = round_decimals(order_total
form_field5.value = extendedcost;
form_field5.value = round_decimals(extendedcos
form_field4.value = form_field5.value
//var newValues = addCommas(form_field5.valu
//form_field3.value = newValues;
//var newValue = addCommas(form_field5.valu
//form_field5.value = newValue;
} else if((unit_cost > 0 && unit_cost != "" && unit_cost !=null) && (extendedcost > 0 && extendedcost != "" && extendedcost !=null)) {
order_total += extendedcost / unit_cost;
form_field1.value = round_decimals(unit_cost, 5);
form_field2.value = order_total;
form_field3.value = extendedcost;
form_field3.value = round_decimals(extendedcos
form_field5.value = form_field3.value
form_field4.value = form_field5.value
//var newValue = addCommas(form_field5.valu
//form_field3.value = newValue;
//form_field5.value = newValue;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Notice that there is no C# or ColdFusion involved anymore....
ASKER
That worked perfectly thanks...I figured it was betetr to be done in js anyways. I wrote that code 4 years ago.