Google Answers Logo
View Question
 
Q: Set Focus or Jump to a specific control web opening a web page -visual basic.net ( Answered 5 out of 5 stars,   3 Comments )
Question  
Subject: Set Focus or Jump to a specific control web opening a web page -visual basic.net
Category: Computers > Programming
Asked by: homebuilder-ga
List Price: $5.00
Posted: 05 Mar 2003 10:05 PST
Expires: 04 Apr 2003 10:05 PST
Question ID: 172178
I am developing a data driven website in visual studio .net 2002 using
Visual Basic and APS.

I am trying to jump to a specific text box when a page loads. I can
Set the tab index to 1 and achive this by hitting the tab key but am
wanting this to load with the cursor on the specified text box.

Clarification of Question by homebuilder-ga on 05 Mar 2003 13:39 PST
THis doesn't lok like VB to me (maybe java?) I need vb code that will
work on the code behind page of an HTML document
Answer  
Subject: Re: Set Focus or Jump to a specific control web opening a web page -visual basic.net
Answered By: maxhodges-ga on 05 Mar 2003 18:56 PST
Rated:5 out of 5 stars
 
homebuilder-ga,

Interesting question! Here's your answer:

onload="javascript:document.all.MyControl.focus()" where MyControl is the ID
of the control to catch focus.

[VISUAL BASIC]
' <summary>
' SetFocus
' Set Focus to a web control
' </summary>
' <param name="System.Web.UI.Control ctrl"></param>

Private Sub SetFocus(ByVal ctrl As System.Web.UI.Control)
	Dim s As String = "<SCRIPT language='javascript'>document.getElementById('" 
		& ctrl.ID & "').focus() </SCRIPT>"
	RegisterStartupScript("focus", s)
End Sub 

Calling SetFocus( )
	Me.SetFocus(Me.TextBox1)

http://www.objectsharp.com/Tips/archive/tip_2002_02_27.asp

Finally, one more approach. This C#, but I am sure you can understand the concept:
this.Controls.Add(new LiteralControl("<Script Language=javascript>"));
this.Controls.Add(new
LiteralControl("document.forms[0].elements[MyControl].focus()"));
this.Controls.Add(new LiteralControl("</Script>"));

I hope this answer solves your problem.
homebuilder-ga rated this answer:5 out of 5 stars
Fantastic, Thankyou very much!

Comments  
Subject: Re: Set Focus or Jump to a specific control web opening a web page -visual basic.net
From: hatchetman-ga on 05 Mar 2003 12:31 PST
 
body onload="document.MyForm.MyTextBox.focus();"
Subject: Re: Set Focus or Jump to a specific control web opening a web page -visual basic.net
From: mikviv-ga on 05 Mar 2003 14:34 PST
 
<HTML>
<HEAD>
</HEAD>

<BODY>
<FORM onsubmit='return false' NAME=myForm>
<INPUT NAME='myInput'>
</FORM>
<script>
document.myForm.myInput.focus()
</script>
</BODY>
</HTML> 
You need to wrap the input in a form element.
The return false on the on submit is needed for not submitting the
form, if you use it as a non-form control, otherwise if the text big
is in a form that must be submitted, remove it.
Subject: Re: Set Focus or Jump to a specific control web opening a web page -visual basic.net
From: colinlarkin-ga on 30 Aug 2004 17:04 PDT
 
onload="javascript:document.all.MyControl.focus()"

I used this one, but IE in XP SP2 (not G-toolbar though) and NS block
it as a Pop-up, is their another way?

(apart from VB and C# as they are over my head)

thanks ~cl

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy