Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use onchange property using javascript in .Net?

Posted on 2009-04-09
5
Medium Priority
?
632 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 1000 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 1000 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
This video teaches users how to migrate an existing Wordpress website to a new domain.
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…

730 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