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.Extended_Cost_#i#.value =="" || document.po_form.Extended_Cost_#i#.value ==null) {
            document.po_form.Extended_Cost_#i#.value=0;
      }      
      if(document.po_form.Unit_Cost_#i#.value =="" || document.po_form.Unit_Cost_#i#.value ==null) {
            document.po_form.Unit_Cost_#i#.value=0;
      }      
      if(document.po_form.Quantity_#i#.value =="" || document.po_form.Quantity_#i#.value ==null) {
            document.po_form.Quantity_#i#.value=0;
      }      
      if(document.po_form.Hidden_Extended_Cost_#i#.value =="" || document.po_form.Hidden_Extended_Cost_#i#.value ==null) {
            document.po_form.Hidden_Extended_Cost_#i#.value=0;
      }      
      if(document.po_form.Inserted_Extended_Cost_#i#.value =="" || document.po_form.Inserted_Extended_Cost_#i#.value ==null) {
            document.po_form.Inserted_Extended_Cost_#i#.value=0;
      }      
      </cfloop>
      
      //again
    // Run through all the form fields

    // Get the current field
    var form_field1 = eval("document.po_form.Unit_Cost_" + num);
      //alert(form_field2);
      var form_field2 = eval("document.po_form.Quantity_" + num);
      var form_field3 = eval("document.po_form.Extended_Cost_" + num);
    var form_field4 = eval("document.po_form.Hidden_Extended_Cost_" + num);
      var form_field5 = eval("document.po_form.Inserted_Extended_Cost_" + num);
    //Get the Unit Cost
    var unit_cost = parseFloat(form_field1.value);
    // Get the Quantity
    var quantity = parseInt(form_field2.value);
      //Get the Extended Cost
      var extendedcost = parseFloat(form_field3.value);
      
      
      
           
      // 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.value);
            //form_field3.value = newValues;
            form_field1.value = round_decimals(unit_cost, 5);
            //var newValue = addCommas(form_field5.value);
            //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(extendedcost, 2);
            form_field4.value = form_field5.value
            //var newValues = addCommas(form_field5.value);
            //form_field3.value = newValues;
            //var newValue = addCommas(form_field5.value);
            //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(extendedcost, 2);
            form_field5.value = form_field3.value
            form_field4.value = form_field5.value
            //var newValue = addCommas(form_field5.value);
            //form_field3.value = newValue;
            //form_field5.value = newValue;
      }
}
asaworkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mrichmonCommented:
well really you should write the code better rather than just convert that code (which is pretty poor) to C#.

Instead of having a server side script write a loop for each field, you should have a function in javascript that takes in the "i" value and works on that.

Then if you do this the code does not care if you are using C#, ColdFusion, or if you later change to PHP, Java, etc..

You then just change the cfloop to be a normal javascript loop over the numbers.  See code below.  Please not that you will need to ensure that you have the ID for each field set whereas I bet the old code only used the name.

You could probably clean up a lot of the code like this, but you only showed a small part.

function SetDefaults(i)
{
	if(document.getElementById("Extended_Cost_" + i).value == "" || document.getElementById("Extended_Cost_" + i).value == null)
	{
		document.getElementById("Extended_Cost_" + i).value = 0;
	}
	if(document.getElementById("Unit_Cost_" + i).value == "" || document.getElementById("Unit_Cost_" + i).value == null)
	{
		document.getElementById("Unit_Cost_" + i).value = 0;
	}
	if(document.getElementById("Quantity_" + i).value == "" || document.getElementById("Quantity_" + i).value == null)
	{
		document.getElementById("Quantity_" + i).value = 0;
	}
	if(document.getElementById("Hidden_Extended_Cost_" + i).value == "" || document.getElementById("Hidden_Extended_Cost_" + i).value == null)
	{
		document.getElementById("Hidden_Extended_Cost_" + i).value = 0;
	}
	if(document.getElementById("Inserted_Extended_Cost_" + i).value == "" || document.getElementById("Inserted_Extended_Cost_" + i).value == null)
	{
		document.getElementById("Inserted_Extended_Cost_" + i).value = 0;
	}
}



function CalculateRowTotal(num) {
     var order_total = 0;
     //make all fields for the order form 0 by default for total reasons
     for(var i = 1; i <=15; i++)
     {
        SetDefaults(i);
     }

     // rest of your code goes here....

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mrichmonCommented:
Notice that there is no C# or ColdFusion involved anymore....
0
asaworkerAuthor Commented:
That worked perfectly thanks...I figured it was betetr to be done in js anyways. I wrote that code 4 years ago.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.