value of textboxes in browser does not match what is in viewsource ?

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
 
M

Mad Scientist Jr

I found a simple workaround. By adding an onchange attribute to a
control, you can force focus to the next control:

dim sJS as string
sJS = "document.Form1." & DropDownList1.UniqueID & ".focus();"
Text1.Attributes.Add("onchange", sJS)

This works if the next control you want to set focus to is a
dropdownlist, but for some reason, if the next control is a textbox:

dim sJS as string
sJS = "document.Form1." & Text2.UniqueID & ".focus();"
Text1.Attributes.Add("onchange", sJS)

when the focus gets set to it you can't type in it. However if you
tab, it tabs correctly to the next control after that. So it is sort
of receiving the focus but not really. When it renders the page .NET
adds a "dopostback" to the onchange javascript. When you view source,
the onchange property is:

onchange="javascript:document.Form1.Text2.focus();__doPostBack('Text1','')"

I think the doPostBack is interfering with the focus command. If I
could somehow specify for my focus command to appear in the onchange
property AFTER the doPostBack, it might work. If anyone can tell me
how to do this, it would be most appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top