Q

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 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

Dig deeper on Win Development Resources

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close