Adding Javascript Select All to ASP.NET Control

A common user-interface technique to updating form fields is to have all the text selected when the user gives it focus. This allows the user the ability to clear out old text easily, without having to use the mouse and/or backspace button to clear out the selection themselves.

<asp:TextBox ID="txtSearch" runat="server" MaxLength="100" />

You can add the javascript code to select the text (this.select()) to the onclick or the onfocus event. Or you can add it to both.

txtSearch.Attributes.Add("onclick", "this.select();");
txtSearch.Attributes.Add("onfocus", "this.select();");
This entry was posted in ASP.NET and tagged , , , . Bookmark the permalink.

Comments are closed.