Utility to automatically highlight text in a textbox when it gets the focus
May 31st, 2009 — Brian FooteHere’s a handy utility which will modify every textbox on a page and add javascript to highlight the text in the textbox when it gets the focus.
/// <summary>
/// Searches through all controls in parameter C and adds javascript to textboxes
/// to automatically select contents when each textbox gets the focus.
/// Example: SetTextBoxesToAutoselect(Page) : Sets all textboxes on current webpage
/// </summary>
/// <param name=”C”></param>
public static void SetTextBoxesToAutoselect(Control C)
{
try
{
foreach (Control c in C.Controls)
{
if (c is TextBox)
{
TextBox tb = (TextBox)c;
tb.Attributes.Add(“onfocus”, “this.select();”);
}
SetTextBoxesToAutoselect(c);
}
}
catch (Exception)
{
//throw;
}
}
