Scroll Page 表单提交后页面重新滚回原来滚动条所在位置

作者scottwater
原文地址:http://scottwater.com/articles/ScrollPage
感谢scottwater先生!

  1#region Disclaimer
  2/**//**********************************************************************
  3Based on presentation by Brad McCabe of Infragistics
  4
  5Updated by Scott Watermasysk (http://scottwater.com)
  6
  7Provided as is, with no warrenty, etc.
  8Please use however you see fit. Just don't ask for a VB version :)
  9***********************************************************************/

 10#endregion

 11
 12using System;
 13using System.IO;
 14using System.Text;
 15using System.Text.RegularExpressions;
 16using System.Web.UI;
 17
 18namespace ShangGu.XaTaxi.Components
 19{
 20    /**//// <summary>
 21    /// Summary description for ScrollPage.
 22    /// </summary>

 23    public class ScrollPage : System.Web.UI.Page
 24    {
 25        public ScrollPage()
 26        {
 27
 28        }

 29
 30        private bool _useScrollPersistence = true;
 31        /**//// <summary>
 32        /// There could be PostBack senarios where we do not want to remember the scroll position. Set this property to false
 33        /// if you would like the page to forget the current scroll position
 34        /// </summary>

 35        public bool UseScrollPersistence
 36        {
 37            get {return this._useScrollPersistence;}
 38            set {this._useScrollPersistence = value;}
 39        }

 40
 41        private string _bodyID;
 42        /**//// <summary>
 43        /// Some pages might already have the ID attribute set for the body tag. Setting this property will not render the ID or change
 44        /// the existing value. It will simply update the javascript written out to the browser.
 45        /// </summary>

 46        public string BodyID
 47        {
 48            get {return this._bodyID;}
 49            set {this._bodyID = value;}
 50        }

 51
 52
 53        //Last chance. Do we want to maintain the current scroll position
 54        protected override void OnPreRender(EventArgs e)
 55        {
 56            if(UseScrollPersistence)
 57            {
 58                RetainScrollPosition();
 59            }

 60            base.OnPreRender (e);
 61        }
        
 62
 63        protected override void Render(HtmlTextWriter writer)
 64        {
 65            //No need processing the HTML if the user does not want to maintain scroll position or already has
 66            //set the body ID value
 67            if(UseScrollPersistence)
 68            {
 69                TextWriter tempWriter = new StringWriter();
 70                base.Render(new HtmlTextWriter(tempWriter));
 71                if(BodyID == null)
 72                {
 73                    writer.Write(Regex.Replace(tempWriter.ToString(),"<body","<body id="thebody" ",RegexOptions.IgnoreCase));
 74                }

 75                //=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 76                //
 77                //2004-9-16 武眉博 修正了因BodyID不空时造成javascript错误的bug
 78                //
 79                //=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 80                else
 81                {
 82                    writer.Write(Regex.Replace(tempWriter.ToString(),"<body","<body id=""+BodyID+"" ",RegexOptions.IgnoreCase));
 83
 84                }

 85            }

 86            else
 87            {
 88                base.Render(writer);
 89            }

 90        }

 91
 92        //=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 93        //
 94        //2004-9-20 武眉博 增加了隐藏文本域"__SCROLLPOS_LEFT",以增加对横向滚动条位置的记忆功能
 95        //
 96        //=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 97        private static string saveScrollPosition = "<script language='javascript'>function saveScrollPosition() {{document.forms[0].__SCROLLPOS_TOP.value = {0}.scrollTop;document.forms[0].__SCROLLPOS_LEFT.value = {0}.scrollLeft ;}}{0}.onscroll=saveScrollPosition;</script>";
 98        private static string setScrollPosition = "<script language='javascript'>function setScrollPosition() {{{0}.scrollTop ="{1}"; {0}.scrollLeft  ="{2}"}}{0}.onload=setScrollPosition;</script>";
 99
100        //Write out javascript and hidden field
101        private void RetainScrollPosition()
102        {
103            RegisterHiddenField("__SCROLLPOS_TOP""0");
104            RegisterHiddenField("__SCROLLPOS_LEFT""0");
105            string __bodyID = BodyID == null ? "thebody" : BodyID;
106            RegisterStartupScript("saveScroll"string.Format(saveScrollPosition,__bodyID));
107
108            if(Page.IsPostBack)
109            {
110                RegisterStartupScript("setScroll"string.Format(setScrollPosition,__bodyID, Request.Form["__SCROLLPOS_TOP"],Request.Form["__SCROLLPOS_LEFT"]));
111            }

112        }

113    }

114
115}

116
posted @ 2005-07-13 11:23  冷杉  阅读(317)  评论(0)    收藏  举报