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’>”);
sb.Append(“document.getElementById(‘”);
sb.Append(controlID);
sb.Append(“‘).focus()”);
sb.Append(“</script>”)

// 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.

Related Resources:

Related posts:

  1. .NET Style String Formatting with jQuery – Javascript Function to Support Token Replacement within a String
  2. Vetor Carro-Focus
  3. Make Printable Page using CSS Print Properties
  4. Sitemap Styler Style your Sitemaps with CSS and Javascript
  5. How To Create Dynamic Scrolling Content Box Using AJAX?