Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old March 25th, 2004, 03:14 PM
Authorized User
Join Date: Jan 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to creative_eye
Default How to set focus on page load

I want to set focus (cursor) on a text field while page loads in ASP.NET. I have tried but failed to do so. Please tell me how can i do this.

Old March 25th, 2004, 04:19 PM
Imar's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

Hi there,

The easiest way to do this,is to use RegisterStartupScript to register a script that will run right before the page is done loading. This script then sets the focus on the required control.

Something like the following (C#) code should work:
RegisterStartupScript("Startup", "<script type=\"text/javascript\">document.forms[0]." + TextBox1.ClientID 
        + ".focus();</script>");
        Here, TextBox1 is the control that needs the focus. Add this code to the Page_Load event of your page, and it should work.



Imar Spaanjaars
Everyone is unique, except for me.
Old March 25th, 2004, 10:34 PM
Authorized User
Join Date: Mar 2004
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts

Or if you don't want to use RegisterStartupScript, you still can use this method.

'copy this code in your before you <html> tag.

<script language="JavaScript">
    function cmdFocus()
        return false;

'After that just include this statement into your <body> tag.


'so your page should look like this

<script language="vb" runat="server">
....anything in your application....

<script language="JavaScript">
    function cmdFocus()
        return false;
<body onload="cmdFocus()">
<form runat="server">

 <asp:textbox id="textbox1" runat="server"/>

Hope this will help you since it works to me

Similar Threads
Thread Thread Starter Forum Replies Last Post
set focus sameer_1981 Intro Programming 1 February 27th, 2007 09:14 AM
Set Focus on a Sub Form Brendan Bartley Access 5 September 9th, 2005 10:49 AM
set focus akibaMaila VB.NET 2002/2003 Basics 1 July 12th, 2005 05:56 PM
how to set focus waley .NET Web Services 1 May 25th, 2004 02:38 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.