Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set focus on a textbox 1

Status
Not open for further replies.

davikokar

Technical User
Joined
May 13, 2004
Messages
523
Location
IT
hallo,
I would like to have the google effect: when someone open a page the focus is automatically on the search textbox.

I did like this:

Code:
sub Page_Load(obj as object, e as eventargs)
   tbSearch.focus()
end sub

And this works on my local server, but as soon as I upload the page it doesn't work anymore... the debugger tell me that focus is not a member of textbox. In the MSDN library I found out that actually there is not a focus method for:
System.Web.UI.WebControls > TextBox Class. But there is one for:
System.Web.UI.MobileControls > TextBox Class ....
So, in the end I don't understand: why it does work locally and not on the remote server... and I don't know how to achieve this. Could I just import the mobilecontrol namespace? my application is a normal website so I don't know if it would makes sense. Does someone knows? thanks
 
Here is a neat little piece of code I got from another forum, it works really well.

Code:
   Public Shared Sub SetFocus(ByVal page As System.Web.UI.Page, ByVal control As System.Web.UI.Control)
        Dim strScript As String = String.Format("document.getElementById('{0}').focus();", control.ClientID)
        strScript = String.Format("<script language=""JavaScript"">{0}</script>", strScript)
        page.RegisterStartupScript("setFocus", strScript)
    End Sub

Then just call the function SetFocus(Me, Me.<control>)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top