• (服务器端的代码的实现)当页面上的某个控件回发时,保持滚动条位置的。


    using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;

    public partial class Email_Default : System.Web.UI.Page
    {
       private bool _useScrollPersistence = true;

        /// <summary>
        /// There could be PostBack senarios where we do not want to remember the scroll position. Set this property to false
        /// if you would like the page to forget the current scroll position
        /// </summary>

        public bool UseScrollPersistence
        {
            get { return this._useScrollPersistence; }
            set { this._useScrollPersistence = value; }
        }

        private string _bodyID;

        /// <summary>
        /// Some pages might already have the ID attribute set for the body tag. Setting this property will not render the ID or change
        /// the existing value. It will simply update the javascript written out to the browser.
        /// </summary>
        public string BodyID
        {
            get { return this._bodyID; }
            set { this._bodyID = value; }
        }

        //Last chance. Do we want to maintain the current scroll position
        protected override void OnPreRender(EventArgs e)
        {
            if (UseScrollPersistence)
            {
                RetainScrollPosition();
            }
            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            //No need processing the HTML if the user does not want to maintain scroll position or already has
            //set the body ID value
            if (UseScrollPersistence && BodyID == null)
            {
                TextWriter tempWriter = new StringWriter();
                base.Render(new HtmlTextWriter(tempWriter));
                writer.Write(Regex.Replace(tempWriter.ToString(), "<body", "<body id=\"thebody\" ", RegexOptions.IgnoreCase));
            }
            else
            {
                base.Render(writer);
            }
        }

        private static string saveScrollPosition = "<script language='javascript'>function saveScrollPosition() {{document.forms[0].__SCROLLPOS.value = {0}.scrollTop;}}{0}.onscroll=saveScrollPosition;</script>";
        private static string setScrollPosition = "<script language='javascript'>function setScrollPosition() {{{0}.scrollTop =\"{1}\";}}{0}.onload=setScrollPosition;</script>";

        //Write out javascript and hidden field
        private void RetainScrollPosition()
        {
            RegisterHiddenField("__SCROLLPOS", "0");
            string __bodyID = BodyID == null ? "thebody" : BodyID;
            RegisterStartupScript("saveScroll", string.Format(saveScrollPosition, __bodyID));

            if (Page.IsPostBack)
            {
                RegisterStartupScript("setScroll", string.Format(setScrollPosition, __bodyID, Request.Form["__SCROLLPOS"]));
            }
        }
    }

  • 相关阅读:
    GPT(4kb硬盘) 单硬盘装变色龙、GAH61MAD2V、ALC887VD、HD6570成功驱动经验(转)
    unable to dequeue a cell with identifier Cell must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
    2013.5.29
    平等博弈
    组合数学
    哈密顿+欧拉图
    差分约束
    11.11
    如何直接跳出多重循环
    摘要:数组练习与部分字符串练习
  • 原文地址:https://www.cnblogs.com/RuiLei/p/328794.html
Copyright © 2020-2023  润新知