How to set focus on ASP.NET control?

You can't set focus on server control from code behind and so your only alternative is to use a client side code to set focus. Yes, you can place javascript snippet in every pages to set focus, or much better you can create a generic server method to accomplish same. Another benefit is that it preserve the source code cleaness for complex pages.

Here is the approach:

First create a method in your utility or helper class.

public void SetFocus(Control control)
    if (control != null)
        System.Text.StringBuilder script = new System.Text.StringBuilder();
        script.Append("<script language='javascript'>");
        script.Append("document.getElementById('" + control.ID + "').focus();");
        Page.RegisterStartupScript("SetFocus", script.ToString());

And call this method in your page. Following is the example to set focus on a text box with ID TextBox1:


Similarly, you can even set focus to other types control like CheckBox, RadioButton, Button etc. as well. Following is the example to set focus on a Button: