Ask the Expert

How do I set focus in the first field of a form?

In ASP.NET, how do I set focus in the first field of a form (on load)?
Well, on the server-side, there's no focus at all, as that's a client feature. So you have to resort to traditional HTML and JavaScript. You can add the following attribute in the <body> element:

<body onload="MyForm.txtFirstInput.focus();">

Optionally, you can emit that code from C# code on the server-side by using the Page.RegisterXXX methods that deal with JavaScript emitting.

This was first published in June 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: