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 <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 last published in June 2003

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.

Start the conversation

Send me notifications when other members comment.

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

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close