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

    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"]));
            }
        }
    }

  • 相关阅读:
    Kali渗透测试工具-netcat
    信息收集工具-dimtry
    Beef xss神器
    Scapy编写ICMP扫描脚本
    全国职业技能大赛信息安全管理与评估-MySQL弱口令利用
    crawler 听课笔记 碎碎念 2 一些爬虫须知的基本常识和流程
    crawler 听课笔记 碎碎念 3 关于python的细枝末节的回顾复习
    关于互信息(Mutual Information),我有些话要说
    最让人头疼的清洗数据过程----选择合适的方式快速命中所需的数据
    利用小虫虫做一枚合格宅男,果然牡丹花下做鬼也风流
  • 原文地址:https://www.cnblogs.com/RuiLei/p/328794.html
Copyright © 2011-2022 走看看