Introduction
In some cases when an ASP.NET page loads the control you need to focus on is not visible because it is further down the page. I have had numerous occasions where a request varible indicates which item on a long list the user is interested in viewing, this script can help by scrolling the particular item into view. 

Code
The following function I have added to a Utils.dll library for gerneral use so is static and needs the current page as a variable.

public class Utils
{
public static void FocusControlOnPageLoad(string ClientID, System.Web.UI.Page page)

{

page.RegisterClientScriptBlock("CtrlFocus",

@"<script> 

function ScrollView()

{
var el = document.getElementById('"+ClientID+@"')if (el != null){el.scrollIntoView();
el.focus();}
}

window.onload += ScrollView;

</script>");

}
}

You can use this as follows:

private void Page_Load(object sender, System.EventArgs e)
{
   Utils.FocusControlOnPageLoad(this.yourcontrol.ClientID, this.Page);
}

相关文章:

  • 2021-06-02
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2021-12-16
  • 2021-06-30
猜你喜欢
  • 2021-05-25
  • 2021-12-11
  • 2021-08-31
  • 2021-07-30
  • 2021-09-28
  • 2021-08-03
相关资源
相似解决方案