偶尔发现了微软的一个示例中的解决方法,代码如下:
在Page_load中调用该函数,另外,在aspx页面中将body的 id 起名为 thebody
private
void
RetainScrollPosition()
{
string saveScrollPosition=string.Empty;
string setScrollPosition=string.Empty;
RegisterHiddenField("__SCROLLPOS", "0");
saveScrollPosition+=("<script language='javascript'>");
saveScrollPosition+=("function saveScrollPosition() {");
saveScrollPosition+=(" document.forms[0].__SCROLLPOS.value = thebody.scrollTop;");
saveScrollPosition+=("}");
saveScrollPosition+=("thebody.οnscrοll=saveScrollPosition;");
saveScrollPosition+=("</script>");
RegisterStartupScript("saveScroll", saveScrollPosition.ToString());
if(Page.IsPostBack)
{
setScrollPosition+=("<script language='javascript'>");
setScrollPosition+=("function setScrollPosition() {");
string[] GetVarStr=Page.Request.Params.GetValues("__SCROLLPOS");
setScrollPosition+=(" thebody.scrollTop = " + GetVarStr[0] +";");
setScrollPosition+=("}");
setScrollPosition+=("thebody.οnlοad=setScrollPosition;");
setScrollPosition+=("</script>");
RegisterStartupScript("setScroll", setScrollPosition.ToString());
}
}
{
string saveScrollPosition=string.Empty;
string setScrollPosition=string.Empty;
RegisterHiddenField("__SCROLLPOS", "0");
saveScrollPosition+=("<script language='javascript'>");
saveScrollPosition+=("function saveScrollPosition() {");
saveScrollPosition+=(" document.forms[0].__SCROLLPOS.value = thebody.scrollTop;");
saveScrollPosition+=("}");
saveScrollPosition+=("thebody.οnscrοll=saveScrollPosition;");
saveScrollPosition+=("</script>");
RegisterStartupScript("saveScroll", saveScrollPosition.ToString());
if(Page.IsPostBack)
{
setScrollPosition+=("<script language='javascript'>");
setScrollPosition+=("function setScrollPosition() {");
string[] GetVarStr=Page.Request.Params.GetValues("__SCROLLPOS");
setScrollPosition+=(" thebody.scrollTop = " + GetVarStr[0] +";");
setScrollPosition+=("}");
setScrollPosition+=("thebody.οnlοad=setScrollPosition;");
setScrollPosition+=("</script>");
RegisterStartupScript("setScroll", setScrollPosition.ToString());
}
}