Setting focus to controls in your ASP.NET application is a part of giving your end users the feel that they have come to expect. Making your web applications act more like Windows applications is a key to success (IMO). While setting focus to controls is a very small part in achieving this, it does get you one step closer to having a polished web application that your users will love to use. It will be taken for granted, but something this small should be since it is expected behavior.
public static void SetFocus(Control control)
StringBuilder sb = new StringBuilder();
Control p = control.Parent;
while (!(p is System.Web.UI.HtmlControls.HtmlForm)) p = p.Parent;
sb.Append("window.onload = SetFocus;\r\n");
window.onload = SetFocus;
But you don't need to care about that because all you need to do to use it is call your static method from your code behind class:
This will work from post-backs or from the page load.
I had some problems with the formatting of the code due to some angle brackets (caused most of the code to disappear). All fixed now. Sorry.