Solved

OnTextChanged Event Not firing

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

696 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