textchanged event not firing

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
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).

<code>
Private Sub txtMileageBeginning_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtMileageBeginning.TextChanged
If (txtMileageBeginning.Text.Trim <> String.Empty) AndAlso (txtEnding.Text.TrimEnd <> String.Empty) Then
txtTotal.Text = txtMileageBeginning.Text.Trim + txtEnding.Text.TrimEnd
End If
End Sub

Private Sub txtEnding_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEnding.TextChanged
If (txtMileageBeginning.Text.Trim <> String.Empty) AndAlso (txtEnding.Text.TrimEnd <> String.Empty) Then
txtTotal.Text = txtMileageBeginning.Text.Trim + txtEnding.Text.TrimEnd
End If
End Sub
</code>

How can I get these events to fire in order to dynamically calculate the total?
 
Web application is different from normal Windows application. I think those events did fired, just not in the way that you expected. TextChanged event only fires DURING post back (As far as I know, all ASP .Net events are like this). Try pressing "Enter" (assuming you have a button for post back) after you filled those two textboxes, txtTotal should change.

The effect you want probably is that as soon as user type in some value, the text in txtTotal will change immediately, right? This kind of effect can only be achieved by client-side scripts (Javascript or VBScript, with "onChange" in this case). Hope this helps.
 
problem executing the javascript

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:

<code>
txtMileageBeginning.Attributes.Add("onChange", "ChkMileage(document.getElementById('" & txtMileageBeginning.ClientID & "'), document.getElementById('" & txtEnding.ClientID & "'), document.getElementById('" & txtTotal.ClientID & "');")

txtEnding.Attributes.Add("onChange", "ChkMileage(document.getElementById('" & txtMileageBeginning.ClientID & "'), document.getElementById('" & txtEnding.ClientID & "'), document.getElementById('" & txtTotal.ClientID & "');")

Dim sbChkMileage As New StringBuilder
sbChkMileage.Append("<SCRIPT language='javascript'>")
sbChkMileage.Append("function ChkMileage(objTextBox1, objTextBox2, objTextBox3) { ")
sbChkMileage.Append("if(objTextBox1.value.length > 0 && objTextBox2.value.length > 0 && document.")
sbChkMileage.Append("getElementById(objTextBox3).value == objTextBox1.value + objTextBox2.value)}")
sbChkMileage.Append("</SCRIPT>")
RegisterStartupScript("PageStartupScriptChkMileage", sbChkMileage.ToString)
sbChkMileage = Nothing
</code>

When I'm debugging and view the source code, I get the following in the View Source:
<code>
onChange="ChkMileage(document.getElementById('txtMileageBeginning'), document.getElementById('txtEnding'), document.getElementById('txtTotal');



<SCRIPT language='javascript'>function ChkMileage(objTextBox1, objTextBox2, objTextBox3) { if(objTextBox1.value.length > 0 && objTextBox2.value.length > 0 && document.getElementById(objTextBox3).value == objTextBox1.value + objTextBox2.value)}</SCRIPT>
</code>

While also debugging, there was no "running document"???

What am I doing wrong, because the javascript is not firing after I place values in the first two textboxes???
 
There are quite a few errors in your code:

1. The two lines where you add attributes to txtMileageBeginning and txtEnding, you are missing a ")" at the very end.

2. The first line of your function is wrong. It reads if the value of the first textbox is greater than 0 and the value of the second textbox is greater than 0 AND the third textbox is equal to the first textbox plus the second textbox (concatenated, see below).

3. Why are you calling getElementById in your function again? objTextBox3 is an object, NOT a textbox ID, since you already called that once when you pass arguements to this function.

4. Where is the body of your "if" block?

5. Missing brackets again at the end of function.

6. In your case, you cannot just add two objects.value, they will be treated as string. You need to use eithe parseInt or parseFloat function.


I suggest you type out the entire code block first before you append it to the string builder, might avoid some obvious mistakes.

Your code should look something like this after correction:
Code:
txtMileageBeginning.Attributes.Add("onChange", ChkMileage(document.getElementById('" & txtMileageBeginning.ClientID & "'), document.getElementById('" & txtEnding.ClientID & "'), document.getElementById('" & txtTotal.ClientID & "'));")

txtEnding.Attributes.Add("onChange", "ChkMileage(document.getElementById('" & txtMileageBeginning.ClientID & "'), document.getElementById('" & txtEnding.ClientID & "'), document.getElementById('" & txtTotal.ClientID & "'));")

Dim sbChkMileage As New StringBuilder
sbChkMileage.Append("<SCRIPT language='javascript'>")
sbChkMileage.Append("function ChkMileage(objTextBox1, objTextBox2, objTextBox3) { ")
sbChkMileage.Append("if(objTextBox1.value.length > 0 && objTextBox2.value.length > 0) {")
sbChkMileage.Append("objTextBox3.value = parseFloat(objTextBox1.value) + parseFloat(objTextBox2.value);}}")
sbChkMileage.Append("</SCRIPT>")
RegisterStartupScript("PageStartupScriptChkMileage", sbChkMileage.ToString)
sbChkMileage = Nothing
 
Back
Top