Solved

How to use onchange property using javascript in .Net?

Posted on 2009-04-09
5
623 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
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…
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 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.

786 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