Set Focus

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If my aspx page contains a listbox and a text box, is there a way to set
focus to the textbox after an item is selected in the listbox?
 
If my aspx page contains a listbox and a text box, is there a way to set
focus to the textbox after an item is selected in the listbox?

txtSomething.Select
 
Obviously MCSD means nothing, select method selects the contents of a text
box, but doesn't set focus to it. That's what the focus method does. And
VBScript only works in IE, while JavaScript works in all browsers:

document.forms["form-name"].elements["text-box-name"].focus();

Jerry
 
Obviously MCSD means nothing, select method selects the contents of a text
box, but doesn't set focus to it. That's what the focus method does. And
VBScript only works in IE, while JavaScript works in all browsers:
document.forms["form-name"].elements["text-box-name"].focus();


I didn't notice that this is asp.net... i thought that this is vb.net where
my sample works.
 
Hi Josip and Jerry
Thanks for the suggestions. However, the focus() method which was tried by
me before I posted this question to the newsgroup is giving some headache to
me. Here is the sample code and I don't know why I am getting the runtime
error '...........null or not an object'.
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

Jerry Pisk said:
Obviously MCSD means nothing, select method selects the contents of a text
box, but doesn't set focus to it. That's what the focus method does. And
VBScript only works in IE, while JavaScript works in all browsers:

document.forms["form-name"].elements["text-box-name"].focus();

Jerry

Josip Medved said:
txtSomething.Select
 
Is your form's id myForm and your input's id txtTest? VS names them Form1
and Text1 by default... Check the page source, to see what's actually being
rendered to the browser, don't rely on the aspx source.

Jerry

Rookie said:
Hi Josip and Jerry
Thanks for the suggestions. However, the focus() method which was tried by
me before I posted this question to the newsgroup is giving some headache
to
me. Here is the sample code and I don't know why I am getting the runtime
error '...........null or not an object'.
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

Jerry Pisk said:
Obviously MCSD means nothing, select method selects the contents of a
text
box, but doesn't set focus to it. That's what the focus method does. And
VBScript only works in IE, while JavaScript works in all browsers:

document.forms["form-name"].elements["text-box-name"].focus();

Jerry

Josip Medved said:
If my aspx page contains a listbox and a text box, is there a way to
set
focus to the textbox after an item is selected in the listbox?

txtSomething.Select
 
My form's id is myForm and textbox' id is txtTest in the aspx page
<FORM id="myForm" name="myForm" method="post" runat="server">
.....
<asp:textbox id="txtTest" style="Z-INDEX: 112; LEFT: 135px; POSITION:
absolute; TOP: 114px" accessKey="T" runat="server" Width="180px"
Height="22px" BackColor="LightBlue" tabIndex="3"></asp:textbox>

In the .vb class file .vb I have:
Protected WithEvents txtTest As System.Web.UI.WebControls.TextBox.

Would I have to declare something similar for the form 'myForm'? By the way,
I cannot refer myForm in the class file as I can do it for the textbox, e.g.
txtTest.Text = "This is a Test" works just fine.
Other javascript calls are running except for this setfocus call.


Jerry Pisk said:
Is your form's id myForm and your input's id txtTest? VS names them Form1
and Text1 by default... Check the page source, to see what's actually being
rendered to the browser, don't rely on the aspx source.

Jerry

Rookie said:
Hi Josip and Jerry
Thanks for the suggestions. However, the focus() method which was tried by
me before I posted this question to the newsgroup is giving some headache
to
me. Here is the sample code and I don't know why I am getting the runtime
error '...........null or not an object'.
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
 
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

Rookie said:
My form's id is myForm and textbox' id is txtTest in the aspx page
<FORM id="myForm" name="myForm" method="post" runat="server">
....
<asp:textbox id="txtTest" style="Z-INDEX: 112; LEFT: 135px; POSITION:
absolute; TOP: 114px" accessKey="T" runat="server" Width="180px"
Height="22px" BackColor="LightBlue" tabIndex="3"></asp:textbox>

In the .vb class file .vb I have:
Protected WithEvents txtTest As System.Web.UI.WebControls.TextBox.

Would I have to declare something similar for the form 'myForm'? By the
way,
I cannot refer myForm in the class file as I can do it for the textbox,
e.g.
txtTest.Text = "This is a Test" works just fine.
Other javascript calls are running except for this setfocus call.


Jerry Pisk said:
Is your form's id myForm and your input's id txtTest? VS names them Form1
and Text1 by default... Check the page source, to see what's actually
being
rendered to the browser, don't rely on the aspx source.

Jerry

Rookie said:
Hi Josip and Jerry
Thanks for the suggestions. However, the focus() method which was tried
by
me before I posted this question to the newsgroup is giving some
headache
to
me. Here is the sample code and I don't know why I am getting the
runtime
error '...........null or not an object'.
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
 
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
 
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,

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
 
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,
Thanks for the insight. Anyway, I tried some other way to fix the problem.
Thanks again
Rookie

Jerry Pisk said:
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
 
Have you tried...

If (Not IsStartupScriptRegistered("jsfocus")) Then
RegisterStartupScript("jsfocus", stScript)
End If

to let it render everything first?
 
Back
Top