页面数据过长,回发时页面滚动条会回到页首的处理.

private void RetainScrollPosition()
  {
   System.Text.StringBuilder saveScrollPosition = new System.Text.StringBuilder();
   RegisterHiddenField("__SCROLLPOS", "0") ;
   saveScrollPosition.Append("<script language='javascript'>") ;
   saveScrollPosition.Append("function saveScrollPosition() {") ;
   saveScrollPosition.Append(" document.forms[0].__SCROLLPOS.value = thebody.scrollTop;") ;
   saveScrollPosition.Append("}") ;
   saveScrollPosition.Append("thebody.onscroll=saveScrollPosition;") ;
   saveScrollPosition.Append("</script>") ;
   RegisterStartupScript("saveScroll", saveScrollPosition.ToString()) ;
   if (Page.IsPostBack)
   {
    System.Text.StringBuilder setScrollPosition = new System.Text.StringBuilder();
    setScrollPosition.Append("<script language='javascript'>");
    setScrollPosition.Append("function setScrollPosition() {");
    setScrollPosition.Append("thebody.scrollTop = " + Request["__SCROLLPOS"] + ";");
    setScrollPosition.Append("}");
    setScrollPosition.Append("thebody.onload=setScrollPosition;");
    setScrollPosition.Append("</script>");
    RegisterStartupScript("setScroll", setScrollPosition.ToString());
   }
  }

注:页面Body 加一个ID = "thebody"
就可以防止在页面中间回发后滚动条回到最上面.


原址: http://www.dvpx.com/index_article_display.aspx?Fid=10&id=85

你可能感兴趣的:(滚动条)