Solved

How to use onchange property using javascript in .Net?

Posted on 2009-04-09
5
624 Views
Last Modified: 2012-05-06
Hi,
I have a VB.Net form with 3 textboxes(amount, fees and total amount).
I am trying to use JavaScript property like onchange to calculate my Total Amount...
but I can't make it to work...it goes not recognize onChange=...
Please could someone help?
Thanks!
Here is my code:
------------------------------
Sub CalculatePmtAmt()

        TotalRef = (CType(txtFees.Text, Double) + CType(txtAmt.Text, Double))
        txtTotalAmt.Text = FormatNumber(TotalRef, 2)

End Sub
------------------------------
<asp:TextBox ID="txtTotalAmt" runat="server" onChange="CalculatePmtAmt" Style="left: 184px; position: absolute;
top: 288px" TabIndex="23"></asp:TextBox>
0
Comment
Question by:vetukna
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:spprivate
ID: 24110811
It wont work that way.
Put this code in page load
txtTotalAmt.Attributes.Add("onchange", "javascript:CalculatePmtAmt();")
0
 
LVL 3

Accepted Solution

by:
OutOfTouch earned 250 total points
ID: 24112319
As far as I know you can't call a code behind method from JavaScript unless you use ajax to make the call and mark the code behind method with correct attribute to make it callable from JavaScript. The codebhind method needs the [WebMethod]
 attribute applied to it and the method needs to be static as well.
Here is a link to a simple example and the second link explains why the page method you are calling needs to marked as static.
http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx
http://encosia.com/2008/04/16/why-do-aspnet-ajax-page-methods-have-to-be-static/
 
0
 
LVL 15

Assisted Solution

by:spprivate
spprivate earned 250 total points
ID: 24112623
oops my bad,I didnt see that method was server side and was thinking as javascript.Yeah Outoftouch is right.
But there is another way.We can attach javascript function to do this math.something like

var tot=document.getelementbyid(txttotal)
tot.value=Parseint(document.getelementbyid(txtfees).value)+Parseint(document.getelementbyid(txtamt).value)
0
 

Author Comment

by:vetukna
ID: 24765372
Thank you for your reply and help...
But I decided to take a more simplified approach...since for some reason my javascript function still did not work.
Here is my code:
It does what I was looking for but there is still one formating problem....

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then

 txtSFFees.Attributes.Add("onchange", "txtTotalAmt.value = parseFloat(txtAmt.value) + parseFloat(txtNSFFees.value)")
 txtAmt.Attributes.Add("onchange", "txtTotalAmt.value = parseFloat(txtAmt.value) + parseFloat(txtNSFFees.value)")
 txtTotalAmt.Attributes.Add("onchange", "txtTotalAmt.value = parseFloat(txtAmt.value) + parseFloat(txtNSFFees.value)")

End If
End Sub

Example:
       Amt:  20.00
   NSFFees:  2.00
   -------------------
 Total Amt:  22

Is there a way how I can make Total Amt to be shown as 22.00 and not 22?
Thank you...
0
 

Author Closing Comment

by:vetukna
ID: 35142591
Thanks!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
OWA - local directory not showing 5 38
how to configure and install nginx ? 1 489
Technology stack suggestions 4 67
Writing and Selling Web App Based on Google Sheets 2 80
      Install BugTracker on Windows 2008 server Step 1:  Install windows 2008 server 32 bit OS and configure IIS. Step 2:  Install SQL server ( SQL server 2005 or SQL server 2005 Express edition. The installer for 2008  version isn’t very f…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question