Solved

How to use onchange property using javascript in .Net?

Posted on 2009-04-09
5
629 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

621 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