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

Discussion in 'ASP .Net' started by Davisro, Apr 20, 2004.

  1. Davisro

    Davisro Guest

    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
     
    Davisro, Apr 20, 2004
    #1
    1. Advertising

  2. Davisro

    Scott Hamlin Guest

    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);
    }


    "Davisro" <NA> wrote in message
    news:#4#...
    > 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
    >
    >
     
    Scott Hamlin, Apr 20, 2004
    #2
    1. Advertising

  3. Davisro

    Davisro Guest

    Scott,

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

    Rog

    "Scott Hamlin" <> wrote in message
    news:...
    > 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);
    > }
    >
    >
    > "Davisro" <NA> wrote in message
    > news:#4#...
    > > 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
    > >
    > >

    >
    >
     
    Davisro, Apr 20, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tina
    Replies:
    4
    Views:
    393
  2. Sam

    DataGrid? Sum Calculate?

    Sam, Jan 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,584
    Eliyahu Goldin
    Jan 21, 2005
  3. Magnus Blomberg
    Replies:
    3
    Views:
    728
    Magnus Blomberg
    Apr 14, 2005
  4. Neo
    Replies:
    0
    Views:
    343
  5. Boss302
    Replies:
    0
    Views:
    1,070
    Boss302
    Nov 21, 2006
Loading...

Share This Page