Home All Groups Group Topic Archive Search About

Calculate inside a form and use the same form as Insert record form

Author
30 Nov 2006 6:49 PM
reidarT
I have 3 fields in an aspx page. The 3. field should be the sum of field A
and field B
I use OnTextChanged to calculate the sum in field3.
At the same time I want to insert the content of theese 3 fields into a row
in a table.
The problem is that I need 'postback is true' on the textfields to get an
immediate calculation and then the insert action is triggered.
reidarT

Author
30 Nov 2006 7:55 PM
Rad [Visual C# MVP]
Hey reidar,

You don't have to do the postback thing. You can use some javascript
to achieve the same result.

Say you have three textboxes:

txtFirst,txtSecond,txtSum

To get txtSum to be calculated do the following:

In the aspx html code add the following function

<script language=javascript>
     function check(){
if(parseInt(document.getElementById('txtFirst').value,10)!="NaN" &&
parseInt(document.getElementById('txtSecond').value,10)!="NaN"){
            document.getElementById('txtSum').value =
parseInt(document.getElementById('txtFirst').value,10) +
parseInt(document.getElementById('txtSecond').value,10)
        }
     }
    </script>

This is basically some javascript that calculate the sum of the first
two textboxes and stores the result in the third. The 10 argument to
parseInt is not redundant ... if you have a number starting with 0 in
either of the text boxes the javascript engine will think it is an
octal number

Then in your code behind do this:

If Not IsPostBack Then
            txtFirst.Attributes.Add("onChange", "check()")
            txtSecond.Attributes.Add("onChange", "check()")
End If



On Thu, 30 Nov 2006 19:49:41 +0100, "reidarT" <rei***@eivon.no> wrote:

>I have 3 fields in an aspx page. The 3. field should be the sum of field A
>and field B
>I use OnTextChanged to calculate the sum in field3.
>At the same time I want to insert the content of theese 3 fields into a row
>in a table.
>The problem is that I need 'postback is true' on the textfields to get an
>immediate calculation and then the insert action is triggered.
>reidarT
>
--

Bits.Bytes.
http://bytes.thinkersroom.com
Author
1 Dec 2006 5:17 AM
Cor Ligthert [MVP]
Reidar,

Doing this with "on text change" is in my idea completely foolish over ASPX.
Beside the advise from Rad, you can have a look what Ajax can do for you.
(Just to experiment, because the advise from Rad is in my idea better).

http://ajax.asp.net/

Cor

Show quoteHide quote
"reidarT" <rei***@eivon.no> schreef in bericht
news:Osy8OBLFHHA.3540@TK2MSFTNGP02.phx.gbl...
>I have 3 fields in an aspx page. The 3. field should be the sum of field A
>and field B
> I use OnTextChanged to calculate the sum in field3.
> At the same time I want to insert the content of theese 3 fields into a
> row in a table.
> The problem is that I need 'postback is true' on the textfields to get an
> immediate calculation and then the insert action is triggered.
> reidarT
>