M
Mad Scientist Jr
I have a textbox control (for simplicity call it Text1) with
autopostback turned on.
It has a couple validators:
RequiredFieldValidator_Text1
RegularExpressionValidator_Text1
The Text1 onchange (see code below) event does these things
1. fires the validators
2. if Text1 is valid,
A) re-calculates some other fields
(say Text4 and Text5)
B) set focus to Text2
3. if Text1 is NOT valid, keep focus on Text1
(and validators display their errors to the user)
The set focus is not working at all.
To set focus, originally I tried both registerstartupscript and
registerclientsidescript and neither worked. However javascript eval
seems to work:
In the HTML, i have:
<body onload="javascript:window.history.go(1);
eval(document.Form1.txtJavascript.value);">
<form id="Form1" method="post" runat="server">
<INPUT id="txtJavascript" type="hidden" runat="server"
NAME="txtJavascript">
then I init the javascript in Page_Load of the codebehind:
if not (IsPostBack) then
' SET FOCUS ON FIRST CONTROL
txtJavascript.Value = "document.Form1." & Text1.UniqueID &
".focus();"
txtTest.Value = "document.Form1." & Text1.UniqueID & ".focus();" '
for testing
end if
this works fine to set the focus to the right control the first time
the form loads.
Now in my Text1 onchange event, i simply should be able to set the
value of txtJavascript to set focus to the proper control depending on
what happens, but it doesn't work.
View source shows that the value of txtJavascript is still the same as
when it was initialized in Page_Load.
So I did a little test to see if the onchange was even changing the
text, so I had it update a visible textbox txtTest to see if anything
changed.
In the browser I see in txtTest that the value IS changed to the new
control:
document.Form1.Text2.focus();
However when I look at the codebehind, txtTest has the old value!
document.Form1.Text1.focus();
So it seems that .NET is somehow changing what is SEEN in the textbox
in the browser, but the source (and what Javascript can see) is not
changed.
Does this make any sense? Can someone help explain what's happening?
-----
'onchange event for Text1:
Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Text1.TextChanged
'TEST CHANGE CONTENTS OF THE HIDDEN JAVASCRIPT BOX
txtJavascript.Value = "document.Form1." & Text2.UniqueID &
".focus();"
'TEST CHANGE CONTENTS OF A VISIBLE TEXT BOX
'JUST TO SEE IF THIS CODE IS CHANGING THE TEXT!
TextTest.Text = "document.Form1." & Text2.UniqueID & ".focus();"
' -----------------------------------------------------------------------------
' VALIDATE THE CONTROL
' so any error messages appear on the screen,
' (otherwise with autopostback, if there is a validation error,
' the messages appear for a second but then disappear
' after the autopostback re-renders the page)
RequiredFieldValidator_Text1.Validate()
RegularExpressionValidator_Text1.Validate()
' -----------------------------------------------------------------------------
' SET FOCUS DEPENDING ON VALIDATION
If RequiredFieldValidator_txtSecondaryAmount.IsValid And
RegularExpressionValidator_txtSecondaryAmount.IsValid Then
' CALCULATE SOME FIELDS BASED ON NEW Text1 VALUE
Call subRecalculate(Text1.text, Text4.text, Text5.text)
' GOTO NEXT CONTROL
txtJavascript.Value = "document.Form1." & Text2.UniqueID &
".focus();"
Else
' STAY AT SAME CONTROL
txtJavascript.Value = "document.Form1." & Text1.UniqueID &
".focus();"
End If
End Sub ' Text1_TextChanged
autopostback turned on.
It has a couple validators:
RequiredFieldValidator_Text1
RegularExpressionValidator_Text1
The Text1 onchange (see code below) event does these things
1. fires the validators
2. if Text1 is valid,
A) re-calculates some other fields
(say Text4 and Text5)
B) set focus to Text2
3. if Text1 is NOT valid, keep focus on Text1
(and validators display their errors to the user)
The set focus is not working at all.
To set focus, originally I tried both registerstartupscript and
registerclientsidescript and neither worked. However javascript eval
seems to work:
In the HTML, i have:
<body onload="javascript:window.history.go(1);
eval(document.Form1.txtJavascript.value);">
<form id="Form1" method="post" runat="server">
<INPUT id="txtJavascript" type="hidden" runat="server"
NAME="txtJavascript">
then I init the javascript in Page_Load of the codebehind:
if not (IsPostBack) then
' SET FOCUS ON FIRST CONTROL
txtJavascript.Value = "document.Form1." & Text1.UniqueID &
".focus();"
txtTest.Value = "document.Form1." & Text1.UniqueID & ".focus();" '
for testing
end if
this works fine to set the focus to the right control the first time
the form loads.
Now in my Text1 onchange event, i simply should be able to set the
value of txtJavascript to set focus to the proper control depending on
what happens, but it doesn't work.
View source shows that the value of txtJavascript is still the same as
when it was initialized in Page_Load.
So I did a little test to see if the onchange was even changing the
text, so I had it update a visible textbox txtTest to see if anything
changed.
In the browser I see in txtTest that the value IS changed to the new
control:
document.Form1.Text2.focus();
However when I look at the codebehind, txtTest has the old value!
document.Form1.Text1.focus();
So it seems that .NET is somehow changing what is SEEN in the textbox
in the browser, but the source (and what Javascript can see) is not
changed.
Does this make any sense? Can someone help explain what's happening?
-----
'onchange event for Text1:
Private Sub Text1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Text1.TextChanged
'TEST CHANGE CONTENTS OF THE HIDDEN JAVASCRIPT BOX
txtJavascript.Value = "document.Form1." & Text2.UniqueID &
".focus();"
'TEST CHANGE CONTENTS OF A VISIBLE TEXT BOX
'JUST TO SEE IF THIS CODE IS CHANGING THE TEXT!
TextTest.Text = "document.Form1." & Text2.UniqueID & ".focus();"
' -----------------------------------------------------------------------------
' VALIDATE THE CONTROL
' so any error messages appear on the screen,
' (otherwise with autopostback, if there is a validation error,
' the messages appear for a second but then disappear
' after the autopostback re-renders the page)
RequiredFieldValidator_Text1.Validate()
RegularExpressionValidator_Text1.Validate()
' -----------------------------------------------------------------------------
' SET FOCUS DEPENDING ON VALIDATION
If RequiredFieldValidator_txtSecondaryAmount.IsValid And
RegularExpressionValidator_txtSecondaryAmount.IsValid Then
' CALCULATE SOME FIELDS BASED ON NEW Text1 VALUE
Call subRecalculate(Text1.text, Text4.text, Text5.text)
' GOTO NEXT CONTROL
txtJavascript.Value = "document.Form1." & Text2.UniqueID &
".focus();"
Else
' STAY AT SAME CONTROL
txtJavascript.Value = "document.Form1." & Text1.UniqueID &
".focus();"
End If
End Sub ' Text1_TextChanged