Solved

OnTextChanged Event Not firing

Posted on 2009-07-15
4
869 Views
Last Modified: 2013-11-08
I have a situation where when a textbox OnTextChange event fires, a javascript function is supposed to calculate age.  It works fine when I do a postback, however I want it to change without doing a postback.  I thought that was the point of putting the javascript function clientside.  I have other javascript that works fine without needing to register the code.  I did not use the AJAX update panel because I do not want to do a postback to execute the code.  The age should be calculated when the textbox looses focus.  I am using Visual Studio 2008, C# and .NET 3.5.  My code is below.
Clientside code:

        function calcDiff() 

        {

            var BDate = document.getElementById("txtBirthdate");

            var ODate = document.getElementById("txtOnsetDate");

            var Disease = document.getElementById("lbDisease");

            var CDCYear = document.getElementById("lbcdcyear");
 

            if ((BDate !== null) && (ODate !== null) && !(Disease == "Lead(child blood)" || Disease == "Hepatitis B, chronic" || Disease == "Hepatitis C, chronic") && parseInt(CDCYear) >= 2009) {

                var years = ODate.getFullYear() - BDate.getFullYear();

                var mos = ODate.getFullMonth() - BDate.getFullMonth();

                document.getElementById("lbAgeCalc").value = "1";

                if (years >= 1) {

                    document.getElementById("txtAge").value = years;

                    document.getElementById("txtAgeMos").value = "";

                    document.getElementById("txtAgeMos").style.backgroundColor = "#ece9d8";

                    document.getElementById("txtAgeMos").disabed = "true";

                    document.getElementById("txtAge").style.backgroundColor = "#ece9d8";

                    document.getElementById("txtAge").disabed = "true";

                }

                else {

                    document.getElementById("txtAgeMos").value = mos;

                    document.getElementById("txtAge").value = "";

                    document.getElementById("txtAge").style.backgroundColor = "#ece9d8";

                    document.getElementById("txtAge").disabed = "true";

                    document.getElementById("txtAgeMos").style.backgroundColor = "#ece9d8";

                    document.getElementById("txtAgeMos").disabed = "true";

                }

            }

            else {

                document.getElementById("txtAge").disabed = "false";

                document.getElementById("txtAgeMos").disabed = "false";

                document.getElementById("txtAge").value = "";

                document.getElementById("txtAgeMos").value = "";

                document.getElementById("lbAgeCalc").value = "0";

            }

            document.all.dvFloatMsg.style.display = "inline";

            document.all.lblMsgDataChanged.style.display = "inline";

            document.all.lblMsgDataSaved.style.display = "none";

            document.all.dvFloatMsg.style.pixelLeft = tbMasterTable.offsetLeft + tbMasterTable.offsetWidth - dvFloatMsg.offsetWidth;

            document.all.dvFloatMsg.style.pixelTop = document.all.dvFloatMenu.style.pixelTop + dvFloatMenu.offsetHeight + 5;

            document.all.lbChangedInfo.style.visibility = "visible";

            document.all.lbSavedInfo.style.visibility = "hidden";

        }

<asp:TextBox ID="txtBirthdate" runat="server" MaxLength="10" Width="90px" CssClass="ObservationText" AutoPostBack="false"></asp:TextBox>

<asp:TextBox ID="txtOnsetDate" runat="server" MaxLength="10" Width="110px" CssClass="ObservationText" AutoPostBack="false"></asp:TextBox>
 

Serverside Code:
 

protected void Page_Load(object sender, System.EventArgs e)

{

   //Lots of other code

  // relevent code

              this.txtBirthdate.Attributes.Add("OnTextChanged", "javascript:calcDiff();");

            this.txtOnsetDate.Attributes.Add("OnTextChanged", "javascript:calcDiff();");

// lots more code

}

Open in new window

0
Comment
Question by:RosalindP
  • 2
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24860748
use "onblur" instead "OnTextChanged"
0
 
LVL 1

Author Comment

by:RosalindP
ID: 24860793
There isn't an OnBlur event for textboxes in DTD XHTML 1.0 Transitional.
0
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 500 total points
ID: 24860975
OnBlur is not an event for asp.net TB Control...but your asp.net contol will be finally rendered to HTML.
onblur is an HTML TB control attibute...
Try the following example and see:

<script language='javascript' type='text/javascript'>
        function display() {
            var tb1 = document.getElementById('<%=TextBox1.ClientID%>').value;
            var tb2 = document.getElementById('<%=TextBox2.ClientID%>');
            tb2.value = tb1;
            return false;
        }
</script><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>

TextBox1.Attributes.Add("onblur", "javascript:return display();");
0
 
LVL 1

Author Closing Comment

by:RosalindP
ID: 31603804
Thank you.  Your explanation was very clear and now I have a better understanding of how the process works.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 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