ASP.NET TidbiTIP #2

HOWTO: Use server side code to set focus onto a control on the client side...

/// <summary>
/// Sets focus to a control on the client side when the page is loaded
/// </summary>
/// <param name="control">The control instance you wish to set the focus on to</param>
protected virtual void SetFocus(System.Web.UI.Control control)
{
    
this.SetFocus(control.ClientID);
}

/// <summary>
/// Sets focus to a control on the client side when the page is loaded
/// </summary>
/// <param name="clientID">The ClientID property for the control you wish to set the focus on to</param>
protected virtual void SetFocus(string clientID)
{
     RegisterStartupScript(
       System.Guid.NewGuid().ToString(),
      
string.Format("<script language='javascript'>document.getElementById('{0}').focus();</script>", clientID));
}

No Comments