在PageLoad中调用下面的方法Position,注意StringBuilder属于System.Text命名空间
private void Position() { RegisterHiddenField("ScrollPostion", "0"); StringBuilder getPosition = new StringBuilder(); getPosition.Append("<script language=javascript>"); getPosition.Append("function get(){"); getPosition.Append("document.forms[0].__SCROLLPOS.value = thebody.scrollTop;"); getPosition.Append("}"); getPosition.Append("thebody.onscroll=saveScrollPosition;"); getPosition.Append("</script>"); ClientScript.RegisterStartupScript(typeof(Page),"savePosition",getPosition.ToString()); if (IsPostBack) { StringBuilder setPosition = new StringBuilder(); setPosition.Append("<script language='javascript'>"); setPosition.Append("function setScrollPosition() {"); setPosition.Append(" thebody.scrollTop = " + Request["ScrollPostion"] + ";"); setPosition.Append("}"); setPosition.Append("thebody.onload=setScrollPosition;"); setPosition.Append("</script>"); RegisterStartupScript("setScroll", setPosition.ToString()); } }
转自:http://blog.163.com/hbsi_zhouwufeng/blog/static/4387253620071114423831/