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

R

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
 
R

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
 
C

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
 

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

Top