Returning page to current position after sending request to server

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
 
A

Ashish M Bhonkiya

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top