textchanged event not firing

Results 1 to 5 of 5

Thread: textchanged event not firing

  1. #1
    Join Date
    Dec 1969
    Posts
    36

    Default textchanged event not firing

    I have two fields on my webform that I want calculated in my code-behind, but as I type in the controls, the events are not firing (as evidenced in debug mode). <BR><BR>&#060;code&#062;<BR>Private Sub txtMileageBeginning_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMileageBeginning.TextChanged<BR> If (txtMileageBeginning.Text.Trim &#060;&#062; String.Empty) AndAlso (txtEnding.Text.TrimEnd &#060;&#062; String.Empty) Then<BR> txtTotal.Text = txtMileageBeginning.Text.Trim + txtEnding.Text.TrimEnd<BR> End If<BR> End Sub<BR><BR> Private Sub txtEnding_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEnding.TextChanged<BR> If (txtMileageBeginning.Text.Trim &#060;&#062; String.Empty) AndAlso (txtEnding.Text.TrimEnd &#060;&#062; String.Empty) Then<BR> txtTotal.Text = txtMileageBeginning.Text.Trim + txtEnding.Text.TrimEnd<BR> End If<BR> End Sub<BR>&#060;/code&#062;<BR><BR>How can I get these events to fire in order to dynamically calculate the total?

  2. #2
    Join Date
    Dec 1969
    Posts
    19,082

    Default RE: textchanged event not firing

    can you do this client-side instead?

  3. #3
    Join Date
    Dec 1969
    Posts
    213

    Default RE: textchanged event not firing

    Do you have AutoPostBack set to True for these controls?<BR><BR>Plus, are you trying to do an addition operation, or a concatenation operation? Because from what you&#039;ve posted, you are going to get a concatenation. That is, if the user enters 400 in txtMileageBeginning and 300 in txtEnding, txtTotal will have 400300, not 700 as I think you intend.<BR><BR>What you need to do instead is<BR>[code language="VB.NET"]<BR>txtTotal.Text = Int32.Parse(txtMileageBeginning.Text.Trim) + Int32.Parse(txtEnding.Text.TrimEnd)<BR>[/code]<BR><BR>HTH

  4. #4
    Join Date
    Dec 1969
    Posts
    36

    Default RE: textchanged event not firing

    I was able to get it working by setting the autopostback property. Howev er, I&#039;d like to avoid a trip to the server...<BR><BR>I tried placing the following logic in my page_load event to handle the client-side jhavascript needed to add the first 2 textboxes to the total:<BR><BR>&#060;code&#062;<BR>txtMileageBeginn ing.Attributes.Add("onChange", "ChkMileage(document.getElementById(&#039;" & txtMileageBeginning.ClientID & "&#039;), document.getElementById(&#039;" & txtEnding.ClientID & "&#039;), document.getElementById(&#039;" & txtTotal.ClientID & "&#039;);")<BR><BR> txtEnding.Attributes.Add("onChange", "ChkMileage(document.getElementById(&#039;" & txtMileageBeginning.ClientID & "&#039;), document.getElementById(&#039;" & txtEnding.ClientID & "&#039;), document.getElementById(&#039;" & txtTotal.ClientID & "&#039;);")<BR><BR> Dim sbChkMileage As New StringBuilder<BR> sbChkMileage.Append("&#060;SCRIPT language=&#039;javascript&#039;&#062;")<BR> sbChkMileage.Append("function ChkMileage(objTextBox1, objTextBox2, objTextBox3) { ")<BR> sbChkMileage.Append("if(objTextBox1.value.length &#062; 0 && objTextBox2.value.length &#062; 0 && document.")<BR> sbChkMileage.Append("getElementById(objTextBox3).v alue == objTextBox1.value + objTextBox2.value)}")<BR> sbChkMileage.Append("&#060;/SCRIPT&#062;")<BR> RegisterStartupScript("PageStartupScriptChkMileage ", sbChkMileage.ToString)<BR> sbChkMileage = Nothing<BR>&#060;/code&#062;<BR><BR>When I&#039;m debugging and view the source code, I get the following in the View Source:<BR>&#060;code&#062;<BR>onChange="ChkMileag e(document.getElementById(&#039;txtMileageBeginnin g&#039;), document.getElementById(&#039;txtEnding&#039;), document.getElementById(&#039;txtTotal&#039;);<BR> <BR><BR><BR>&#060;SCRIPT language=&#039;javascript&#039;&#062;function ChkMileage(objTextBox1, objTextBox2, objTextBox3) { if(objTextBox1.value.length &#062; 0 && objTextBox2.value.length &#062; 0 && document.getElementById(objTextBox3).value == objTextBox1.value + objTextBox2.value)}&#060;/SCRIPT&#062;<BR>&#060;/code&#062;<BR><BR>While also debugging, there was no "running document"???<BR><BR>What am I doing wrong, because the javascript is not firing after I place values in the first two textboxes???


  5. #5
    Join Date
    Dec 1969
    Posts
    96,118

    Default TCH TCH! Missing paren!

    Look what you said:<BR><BR>When I&#039;m debugging and view the source code, I get the following in the View Source:<BR><BR>onChange="ChkMileage(document.getEl ementById(&#039;txtMileageBeginning&#039;), document.getElementById(&#039;txtEnding&#039;), document.getElementById(&#039;txtTotal&#039;);<BR><BR>Go count the left and right parens in that code. Don&#039;t match, do they?<BR><BR>&#039;nuff said?<BR><BR>Might I suggest a simplification or two?<BR><BR>Why do the document.getElementById() calls in the onChange?? Why not do them in the function?<BR><BR>OH WOW! Look at the goof you have!!! For objTextBox3, you *do* the getElementById in *both* the onChange *AND* in the ChkMileage function!!! INCONSISTENCY!<BR><BR>And you know, there&#039;s no real reason that you need to use RegisterStartupScript() for this, since you don&#039;t particularly care where it appears in the HTML page. <BR><BR>Personally, I&#039;d just do:<BR><BR>&#060;form runat=server ...&#062;<BR>...<BR>&#060;/form&#062;<BR>&#060;SCRIPT Language="JavaScript"&#062;<BR>function ChkMileage(name1, name2, name3) <BR>{ <BR> var tb1 = document.getElementById(name1);<BR> var tb2 = document.getElementById(name2);<BR> var tb3 = document.getElementById(name3);<BR><BR> if( tb1.value.length &#062; 0 <BR> && tb2.value.length &#062; 0 <BR> && parseFloat(tb3.value) == parseFloat(tb1.value) + parseFloat(tb2.value)<BR> )<BR> {<BR> ??? what goes here? You have nothing at all in the code you show?<BR> }<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>and then use<BR><BR>txtEnding.Attributes.Add( "onChange", _<BR> "ChkMileage(&#039;" & txtMileageBeginning.ClientID & "&#039;,&#039;" & txtEnding.ClientID & "&#039;,&#039;" & txtTotal.ClientID & "&#039;);")<BR><BR>Although, given that you know the names, I&#039;d be tempted to shorten that to simply:<BR><BR>txtEnding.Attributes.Add( "onChange", _<BR> "ChkMileage(&#039;txtMileageBeginning&#039;,&#039; txtEnding&#039;,&#039;txtTotal&#039;);")<BR><BR>Or , for that matter, forget the arguments to ChkMileage altogether.<BR><BR>But anyway, your entire ChkMileage function (a) makes no sense and (b) isn&#039;t even valid JS code, since it just does an if with no statement after the if.<BR><BR>Figure out what you want ChkMileage to do and then let&#039;s fix it all up the simplest way possible.<BR><BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •