Returning page to current position after sending request to server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am creating a form, and when a user clicks on the ASP.NET calendar control, or on another checkbox that sends a request to the server, the page reloads fine, except that it is at the top of the page, instead of where it was originally. Is there an easy way to move the page back to the current position? or does it have to be individually coded. If individually coded, how might that look? thank

Devin
 
Hi Devin,

you can use this function call the RetainScrollLocation in the pageload and
everything will work fine.

HTH
Regards
Ashish M Bhonkiya


private void RetainScrollLocation()
{
StringBuilder myScriptCodeSave = new StringBuilder();
RegisterHiddenField("_SCROLLLOCATION_", "0");

myScriptCodeSave.Append("<script language='javascript'>");
myScriptCodeSave.Append("function saveScrollLocation() {");
myScriptCodeSave.Append("
document.forms[0]._SCROLLLOCATION_.value = document.body.scrollTop;");
myScriptCodeSave.Append("}");

myScriptCodeSave.Append("document.body.onscroll=saveScrollLocation;");
myScriptCodeSave.Append("</script>");

RegisterStartupScript("saveScrollLocation",
myScriptCodeSave.ToString());

if (Page.IsPostBack)
{

StringBuilder myScriptCodeSet = new StringBuilder();

myScriptCodeSet.Append("<script language='javascript'>");
myScriptCodeSet.Append("function setScrollLocation() {");
myScriptCodeSet.Append(" document.body.scrollTop = " +
Request["_SCROLLLOCATION_"] + ";");
myScriptCodeSet.Append("}");

myScriptCodeSet.Append("document.body.onload=setScrollLocation;");
myScriptCodeSet.Append("</script>");

this.RegisterStartupScript("setScrollLocation",
myScriptCodeSet.ToString());

}

}

Devin said:
I am creating a form, and when a user clicks on the ASP.NET calendar
control, or on another checkbox that sends a request to the server, the page
reloads fine, except that it is at the top of the page, instead of where it
was originally. Is there an easy way to move the page back to the current
position? or does it have to be individually coded. If individually coded,
how might that look? thanks
 
Back
Top