Select TextBox on Page Load

Select TextBox on Page Load 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 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:-
<body onload="document.form.txtName.focus();">
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:-
<body onload="document.form.txtName.focus();">
<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.

39 people like this article, click here to say you like this article too (39)
Send a link to this page via email Send a link to this page