Solved

OnTextChanged Event Not firing

Posted on 2009-07-15
4
880 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

856 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