Search:
Namespaces
Discussions
.NET v1.1
Feedback
Set Focus
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.framework
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Rookie (VIP)
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?
Reply to this message...
Josip Medved
[Original message clipped]
txtSomething.Select
--
Pozdrav,
Josip Medved, MCSD
http://www.jmedved.com
Reply to this message...
Jerry Pisk
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" <
Click here to reveal e-mail address
> wrote in message
news:cgn2ua$dr4$
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Rookie (VIP)
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" wrote:
[Original message clipped]
Reply to this message...
Jerry Pisk
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" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Rookie (VIP)
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" wrote:
[Original message clipped]
Reply to this message...
Jerry Pisk
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" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Rookie (VIP)
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
"Jerry Pisk" wrote:
[Original message clipped]
Reply to this message...
Jerry Pisk
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
"Rookie" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Rookie (VIP)
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" wrote:
[Original message clipped]
Reply to this message...
Jerry Pisk
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" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Rookie (VIP)
Hi Jerry,
Thanks for the insight. Anyway, I tried some other way to fix the problem.
Thanks again
Rookie
"Jerry Pisk" wrote:
[Original message clipped]
Reply to this message...
Josip Medved
[Original message clipped]
I didn't notice that this is asp.net... i thought that this is vb.net where
my sample works.
--
Pozdrav,
Josip Medved, MCSD
http://www.jmedved.com
Reply to this message...
Jerry Pisk
Yes, all input elements have the focus method. Just call it.
Jerry
"Rookie" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
System.EventArgs
System.Object
System.Web.UI.Page
System.Web.UI.WebControls.TextBox
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com