Select TextBox on Page Load
Thursday 05 October 2006
To automatically have a textbox selected on page load, so your visitors can simply start typing without having to select the textbox first.
The procedure to do this is straight forward.
Make sure your asp.net vb page has form tags inserted. The form must have an id, for this I keep it simple and call it form. Don't forget the runat="server" property also.
Then add onload="document.form.txtName.focus();" to the body tag of the page, so it looks a little like below:-
Notice where I've put txtName, that's the name of the asp:textbox that I want to be selected on startup.
A full example of this would be:-
<form id="form" runat="server">
<asp:TextBox id="txtName" runat="server">
I find the above most useful on contact us pages, please see our contact us
page to see a working version of this.