OnTextChanged Event Not firing

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

LVL 1
RosalindPAsked:
Who is Participating?
 
guru_samiCommented:
use "onblur" instead "OnTextChanged"
0
 
RosalindPAuthor Commented:
There isn't an OnBlur event for textboxes in DTD XHTML 1.0 Transitional.
0
 
guru_samiCommented:
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
 
RosalindPAuthor Commented:
Thank you.  Your explanation was very clear and now I have a better understanding of how the process works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.