I have a page where some products and textfields where the user enters a number. I first use JavaScript to calculate the total cost. Based on how many users they enter they get a different rate (as shown in code below). When the user types or paste a number into a textfield the function CalculateCost gets called which calls the other functions (only showing two in the example, CDCOst and DVDCost) to make sure the fields Monthly Cost and Annual Cost are displaying the correct value.
I of course want to do the final calculation in the code behind as well before I insert into the database. How can I achieve something similiar in C#?
function CDCost() {
var monthlyAmount;
var annualAmount;
var amount;
var users = $('#txtCD').val();
if (users > 0 && users < 100) {
amount = users * 14.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return [monthlyAmount, annualAmount];
}
if (users >= 100 && users <= 250) {
amount = users * 12.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return [monthlyAmount, annualAmount];
}
if (users == 0) {
monthlyAmount = 0;
annualAmount = 0;
return [monthlyAmount, annualAmount];
}
}
function DVDCost() {
var monthlyAmount;
var annualAmount;
var amount;
var users = $('#txtDVD').val();
if (users > 0 && users < 100) {
amount = users * 16.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return [monthlyAmount, annualAmount];
}
if (users >= 100 && users <= 250) {
amount = users * 14.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return [monthlyAmount, annualAmount];
}
if (users == 0) {
monthlyAmount = 0;
annualAmount = 0;
return [monthlyAmount, annualAmount];
}
}
function CalculateCost() {
var cd = CDCost();
var dvd = DVDCost();
var monthlyCost = cd[0] + dvd[0];
var annualCost = cd[1] + dvd[1];
return [monthlyCost, annualCost];
}
$('#txtCD').bind('keyup change', function (ev) {
var cost = CalculateCost();
var monthly = cost[0];
var annual = cost[1];
$('#MonthlyCostSum').text(monthly);
$('#AnnualCostSum').text(annual)
});
How would I go on doing this in C#?
Something like:
protected double CDCost()
{
double monthlyAmount;
double annualAmount;
double amount;
double users = Convert.ToDouble(txtCD.Text);
if (users > 0 && users < 100)
{
amount = users * 14.95;
monthlyAmount = amount;
annualAmount = monthlyAmount * 12;
return //how do I return the values here to a CalculateCost function?
}
}