Solved

How to use onchange property using javascript in .Net?

Posted on 2009-04-09
5
618 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Timeout Expired 2 91
How to integrate SSO service with client SSO 10 163
Reducing the size of certificate chain 2 40
Video on my site 4 25
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now