Management of control focus is one of the common tasks when building web applications with effective and friendly user interface. In order to set focus on a certain control such as textboxes, buttons dropdowns after postback / after submit / on Page Load in ASP.NET 1.x, we can use a dynamic javascript block that facilitates Javascript’s focus() function.

private void SetFocus(String controlID)
  // Build the JavaScript String
  System.Text.StringBuilder sb = new System.Text.StringBuilder();
  sb.Append("<script language='javascript'>");
  // Register the script code with the page.
  Page.RegisterStartupScript("FocusScript", sb.ToString());

For the above code, you need to pass the control’s id as the parameter, then define the Javascript function in a string variable then call the Page class to register the script.

Want automatic updates? Subscribe to our RSS feed or
Get Email Updates sent directly to your inbox!

Currently rated 4.0 by 4 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


October 22. 2009 17:34

Social comments and analytics for this post

This post was mentioned on Twitter by bloggingdev: Set Focus After PostBack / After Submit / on Page Load in ASP.NET 1.x - Setting Focus to an ASP.NET Control - http://su.pr/2ir2gk

uberVU - social comments | Reply

December 26. 2009 19:29

I guess there's always an easier way ...

cash loans | Reply

February 27. 2010 19:47

Interesting. One of the things which often strikes me concerning insurance, whether it's auto, household or health, is the effect that convictions have. Clearly for car insurance, that is understandable, but is it right to hit people in other insurance areas for their legal transgressions.

Conviction Insurance | Reply

Add comment

(Will not be displayed!)