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

作者 scottwater
原文地址: http://scottwater.com/articles/ScrollPage
感谢 scottwater先生!
我做了一点修改,使其支持横向滚动条位置的记忆
ContractedBlock.gif ExpandedBlockStart.gif Disclaimer #region Disclaimer
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//**********************************************************************
InBlock.gifBased on presentation by Brad McCabe of Infragistics
InBlock.gif
InBlock.gifUpdated by Scott Watermasysk (
http://scottwater.com)
InBlock.gif
InBlock.gifProvided as is, with no warrenty, etc.
InBlock.gifPlease use however you see fit. Just don't ask for a VB version :)
ExpandedSubBlockEnd.gif**********************************************************************
*/

ExpandedBlockEnd.gif
#endregion

None.gif
None.gif
using  System;
None.gif
using  System.IO;
None.gif
using  System.Text;
None.gif
using  System.Text.RegularExpressions;
None.gif
using  System.Web.UI;
None.gif
None.gif
namespace  ShangGu.XaTaxi.Components
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for ScrollPage.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class ScrollPage : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public ScrollPage()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private bool _useScrollPersistence = true;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// There could be PostBack senarios where we do not want to remember the scroll position. Set this property to false
InBlock.gif        
/// if you would like the page to forget the current scroll position
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool UseScrollPersistence
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return this._useScrollPersistence;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{this._useScrollPersistence = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _bodyID;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Some pages might already have the ID attribute set for the body tag. Setting this property will not render the ID or change
InBlock.gif        
/// the existing value. It will simply update the javascript written out to the browser.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string BodyID
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gif{return this._bodyID;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{this._bodyID = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
//Last chance. Do we want to maintain the current scroll position
InBlock.gif
        protected override void OnPreRender(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(UseScrollPersistence)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                RetainScrollPosition();
ExpandedSubBlockEnd.gif            }

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

InBlock.gif                
//=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
InBlock.gif                
//
InBlock.gif                
//2004-9-16 武眉博 修正了因BodyID不空时造成javascript错误的bug
InBlock.gif                
//
InBlock.gif                
//=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
InBlock.gif
                else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    writer.Write(Regex.Replace(tempWriter.ToString(),
"<body","<body id=""+BodyID+"",RegexOptions.IgnoreCase));
InBlock.gif
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.Render(writer);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
InBlock.gif        
//
InBlock.gif        
//2004-9-20 武眉博 增加了隐藏文本域"__SCROLLPOS_LEFT",以增加对横向滚动条位置的记忆功能
InBlock.gif        
//
InBlock.gif        
//=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
InBlock.gif
        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}.οnscrοll=saveScrollPosition;</script>";
InBlock.gif        
private static string setScrollPosition = "<script language='javascript'>function setScrollPosition() {{{0}.scrollTop ="{1}"; {0}.scrollLeft  ="{2}"}}{0}.οnlοad=setScrollPosition;</script>";
InBlock.gif
InBlock.gif        
//Write out javascript and hidden field
InBlock.gif
        private void RetainScrollPosition()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            RegisterHiddenField(
"__SCROLLPOS_TOP""0");
InBlock.gif            RegisterHiddenField(
"__SCROLLPOS_LEFT""0");
InBlock.gif            
string __bodyID = BodyID == null ? "thebody" : BodyID;
InBlock.gif            RegisterStartupScript(
"saveScroll"string.Format(saveScrollPosition,__bodyID));
InBlock.gif
InBlock.gif            
if(Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                RegisterStartupScript(
"setScroll"string.Format(setScrollPosition,__bodyID, Request.Form["__SCROLLPOS_TOP"],Request.Form["__SCROLLPOS_LEFT"]));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/huobazi/archive/2004/09/20/ScrollPage.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值