Setting Focus to an ASP.NET Control – Set Focus After PostBack – After Submit – on Page Load in ASP.NET 1.x
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:
