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

作者scottwater
原文地址:http://scottwater.com/articles/ScrollPage
感谢scottwater先生!
我做了一点修改,使其支持横向滚动条位置的记忆
Disclaimer

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

namespace ShangGu.XaTaxi.Components
{
    
/// <summary>
    
/// Summary description for ScrollPage.
    
/// </summary>

    public class ScrollPage : System.Web.UI.Page
    
{
        
public ScrollPage()
        
{

        }


        
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)
            
{
                TextWriter tempWriter 
= new StringWriter();
                
base.Render(new HtmlTextWriter(tempWriter));
                
if(BodyID == null)
                
{
                    writer.Write(Regex.Replace(tempWriter.ToString(),
"<body","<body id="thebody" ",RegexOptions.IgnoreCase));
                }

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

                }

            }

            
else
            
{
                
base.Render(writer);
            }

        }


        
//=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        
//
        
//2004-9-20 武眉博 增加了隐藏文本域"__SCROLLPOS_LEFT",以增加对横向滚动条位置的记忆功能
        
//
        
//=++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        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>";
        
private static string setScrollPosition = "<script language='javascript'>function setScrollPosition() {{{0}.scrollTop ="{1}"; {0}.scrollLeft  ="{2}"}}{0}.onload=setScrollPosition;</script>";

        
//Write out javascript and hidden field
        private void RetainScrollPosition()
        
{
            RegisterHiddenField(
"__SCROLLPOS_TOP""0");
            RegisterHiddenField(
"__SCROLLPOS_LEFT""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_TOP"],Request.Form["__SCROLLPOS_LEFT"]));
            }

        }

    }


}

Tag标签: .net,asp.net

posted on 2004-09-20 13:55 武眉博<活靶子.Net> 阅读(6260) 评论(16)  编辑 收藏 网摘 所属分类: Asp.Net控件/组件开发

评论

#1楼  2004-10-18 13:38 OSCAR      

很有帮助,谢谢   回复  引用  查看    

#2楼  2004-11-02 23:10 Yok      

Page指令设SmartNavigation=true不就行了?   回复  引用  查看    

#3楼  2004-11-03 14:06 huobazi      

To Yok:
SmartNavigation=true
会有种种不便,起码在.Net1.1种SmartNavigation是有很多bug的
  回复  引用  查看    

#4楼  2004-11-08 13:27 ximi      

如果要记住<DIV></DIV>内的滚动条的位置该怎么办?
  回复  引用  查看    

#5楼  2005-01-02 13:50 cwx99      

很好,不再出现弹不出alert窗口的情况。

writer.Write(Regex.Replace(tempWriter.ToString(),"<body","<body id="thebody" ",RegexOptions.IgnoreCase));
不过像类似这句的语句要把"thebody"改成'thebody'

谢谢!
  回复  引用  查看    

#6楼  2005-02-18 15:07 yihua      

该怎么用这段代码呢?小弟不懂,没有用过组件
那位大哥辛苦一下,帮帮小弟
qq: 252666401
  回复  引用  查看    

#7楼  2005-08-24 16:31 刘 [未注册用户]

就是啊,类也建完了,怎么用啊
  回复  引用    

#8楼 [楼主] 2006-05-30 16:47 武眉博<活靶子.Net>      

关于使用:
所有 aspx.cs 从该类继承就可以了
  回复  引用  查看    

#9楼  2006-06-30 10:38 高海东      

  回复  引用  查看    

#10楼  2006-07-21 14:58 c#新手中的新手 [未注册用户]

请问继承御该如何作用该类,继承御还是不会用,能提供使用卢例吗?c#新手上路,没用过组件,请知道的帮忙一下,谢谢!   回复  引用    

#11楼 [楼主] 2006-07-23 18:14 武眉博<活靶子.Net>      

继承@c#新手中的新手
你的page从这里继承就不用再做任何工作了.
  回复  引用  查看    

#12楼  2006-07-25 15:56 c#新手中的新手 [未注册用户]

请问该段是要贴到class里面还是webpage里然御再继承它?我二者都试过,继承御完全没任何反应,而且网页发生错误,如果不用再做任何工作?记忆位置的物件在那里取得呢?   回复  引用    

#13楼  2006-07-25 16:10 c#新手中的新手 [未注册用户]

我写一个page是每隔十秒自舅refresh的,请问该方法适用refresh吗?如果我继承御,要怎麽使用记忆位置的物件,说再说明一下,谢谢   回复  引用    

#14楼  2006-07-25 16:26 c#新手中的新手 [未注册用户]

请问大大的意思是不是指继承御会有像"SmartNavigation=true"的效果,如果是的话,那对refresh语法好像也没用,我在html下refresh语法卷届仍是会跑回去 @@~   回复  引用    

#15楼  2006-08-10 16:13 yjl.net(abc8103.net@gmail.com) [未注册用户]

如何用代码实现在ListBox(web)中让垂直滚动条滚动到选定位置   回复  引用    


发表评论



姓名 [登录] [注册] 
主页
Email (仅博主可见) 
验证码 *  验证码看不清,换一张
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论   新用户注册   返回页首      

导航: 网站首页 社区 新闻 博问 闪存 网摘 招聘 .NET频道 知识库 找找看 Google站内搜索



China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
China-Pub 计算机绝版图书按需印刷服务

相关文章:

相关链接:
 

导航

公告

广告开始
广告结束

本blog文字只代表靶子某一时间内的观点或结论,与本人所在公司没有任何关系,对其的正确性没有任何担保或假设,如要发表评论请先看清楚日期。 另:本人有权认定一个回复是否包含广告或者其他恶意内容并删除,谢谢!。

统计

与我联系

搜索

 

留言簿

我参加的小组

我参与的团队

我的标签

随笔分类(189)

随笔档案(151)

相册

收藏夹(344)

常去链接

我的东西

友情博客

积分与排名

最新评论

阅读排行榜

评论排行榜