No it doesn't. You're calling it from the server side handler, rendering the
script into the page at a place where the form is not parsed. I got a little
confused here, I thought you want to handle this on the client side, not
through a postback. You should put the script either into the client side
handler or render it after the form is closed. I don't like the whole
postback architecture in Asp.Net so I won't be able to help you much here.
But if you want to do this using HTML and client side scripts it's really
simple, all you need is to render something like this:
<select onchange="document.forms['myForm'].elemwnts['txtTest'].focus();">
<!-- Your options are here -->
</select>
<input type="text" name="txtTest" />
Jerry
Rookie said:
Hi Jerry,
This is what I posted in my second message:
Private Sub lstMyList_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles lstMyList.SelectedIndexChanged
<Here goes my listbox vb code where I used dataset and blah blah blah and
then...>
Dim stScript As String
stScript = "<script language=""JavaScript"">"
stScript +=
"document.forms[""myForm""].elements[""txtTest""].focus();"
stScript += "<"
stScript += "/"
stScript += "script>"
If (Not IsClientScriptBlockRegistered("jsfocus")) Then
RegisterClientScriptBlock("jsfocus", stScript)
End If
End Sub
Doesn't it mean I am calling it from the listbox's event handler?
Rookie
Jerry Pisk said:
So you're saying you're calling it from your listbox's event handler?
That's
not what you posted. Either make that code the listbox's event handler
(onchange probably) or put it in a function and call it when ready. The
code
you posted does neither, it simply renders the script to be executed as
the
form is being loaded, before all the contents is processed - which is
going
to fail.
It seems that you might want to do some research on how client scripts
work
and when they execute...
Jerry
Hi Jerry,
I am doing some task with the listbox before I call the setfocus. If I
put
setfocus function after the form's closing tag, I need to set onclick
for
listbox to the function. This is OK if I dont do anything with the
listbox
except for setting focus to the textbox. However, I perform some data
operations before I want to set focus to the textbox. How do I do that
unless
I render the script dynamically?
Rookie
:
I found the problem - Page.RegisterClientScriptBlock causes the script
to
be
rendered just after the opening form tag. At that point the browser
doesn't
know about any elements inside the form. You need to render the script
after
your form's closing tag. Just put the script into the page, there's
really
no need to render it dynamically.
Jerry