Postback and scroll position
-
Some of our users are complaining that everytime they submit something the page is positioned back to top of the page. How can I preserver scroll position on postbacks? Thanks in advance. Norman Fung
Hi Norman, In ASP.NET 2.0, you don't need to care about this issue as the ASP.NET 2.0 automatically does that for you, hopefully this feature will be supported in the release. Until then, you'll have to provide some code to do that with ASP.NET 1.x. The sample code is something like this:
private void RegisterFocusScript(System.Web.UI.Control control)
{
string script = "<script language=\"javascript\">";
script += "window.document.getElementById(\"" + control.ClientID + "\").focus();";
script += "</script>";if(!this.IsStartupScriptRegistered("clientFocusScript")) this.RegisterStartupScript("clientFocusScript", script);
}
You can call this method in a post back event's handler of a control like
SelectedIndexChanged
,TextChanged
,Click
....private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
RegisterFocusScript(DropDownList1);
} -
Hi Norman, In ASP.NET 2.0, you don't need to care about this issue as the ASP.NET 2.0 automatically does that for you, hopefully this feature will be supported in the release. Until then, you'll have to provide some code to do that with ASP.NET 1.x. The sample code is something like this:
private void RegisterFocusScript(System.Web.UI.Control control)
{
string script = "<script language=\"javascript\">";
script += "window.document.getElementById(\"" + control.ClientID + "\").focus();";
script += "</script>";if(!this.IsStartupScriptRegistered("clientFocusScript")) this.RegisterStartupScript("clientFocusScript", script);
}
You can call this method in a post back event's handler of a control like
SelectedIndexChanged
,TextChanged
,Click
....private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
RegisterFocusScript(DropDownList1);
} -
Some of our users are complaining that everytime they submit something the page is positioned back to top of the page. How can I preserver scroll position on postbacks? Thanks in advance. Norman Fung