Calculate Running sum of 4 text boxes on client side script?

D

Davisro

I am wondering if it is possible to have a running total of four textboxes
so that when any text box is changed I could then calcuate the total of the
four boxes and show this on the webform.

Currenty I hvae four webform textboxes. I collect dollar abounts and want
to show the total as they change from box to box with some client side
script.

I then will insert these numbers with other text boxes into a database via
an insert statement.

Thanks,

Rog
 
S

Scott Hamlin

I do something like this. (It's probably not the most efficient, but it
works.) Each of the controls that will dynamically updating the total uses
the RegisterStartupScript, like so:

txtSubHalfDaysRate.Attributes.Add("onblur",
"javascript:calculateTotal(this.name);")

Anyway, onto the main function:

function calculateTotal(item)
{
if (item == "txtFirstHalfDayNumber" || item == "txtFirstHalfDayRate")
{
document.Form1.txtFirstHalfDaySum.value =
document.Form1.txtFirstHalfDayNumber.value *
document.Form1.txtFirstHalfDayRate.value;
calculateAllFields();
}
if (item == "txtFirstFullDayNumber" || item == "txtFirstFullDayRate")
{
document.Form1.txtFirstFullDaySum.value =
document.Form1.txtFirstFullDayNumber.value *
document.Form1.txtFirstFullDayRate.value;
calculateAllFields();
}
if (item == "txtSubHalfDaysNumber" || item == "txtSubHalfDaysRate")
{
document.Form1.txtSubHalfDaysSum.value =
document.Form1.txtSubHalfDaysNumber.value *
document.Form1.txtSubHalfDaysRate.value;
calculateAllFields();
}
if (item == "txtSubFullDaysNumber" || item == "txtSubFullDaysRate")
{
document.Form1.txtSubFullDaysSum.value =
document.Form1.txtSubFullDaysNumber.value *
document.Form1.txtSubFullDaysRate.value;
calculateAllFields();
}
if (item == "txtMiles" || item == "txtMilesRate")
{
//document.Form1.txtMilesSum.value = document.Form1.txtMiles.value *
document.Form1.txtMilesRate.value;
document.Form1.txtMilesSum.value =
Math.round((document.Form1.txtMiles.value *
document.Form1.txtMilesRate.value)*Math.pow(10,2))/Math.pow(10,2);
}
}


And here is the calculateAllFields function:

function calculateAllFields()
{
document.Form1.txtTotal.value =
parseFloat(document.Form1.txtFirstHalfDaySum.value) +
parseFloat(document.Form1.txtFirstFullDaySum.value) +
parseFloat(document.Form1.txtSubHalfDaysSum.value)
+ parseFloat(document.Form1.txtSubFullDaysSum.value);
}
 
D

Davisro

Scott,

Thanks so much. This worked perfect. I have been looking for this kind of
stuff for a few days now.

Rog
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top