Scroll Page 表单提交后页面重新滚回原来滚动条所在位置
来源:互联网 发布:中国大学生软件杯题目 编辑:程序博客网 时间:2024/04/29 23:52
作者scottwater
原文地址:http://scottwater.com/articles/ScrollPage
感谢scottwater先生!
我做了一点修改,使其支持横向滚动条位置的记忆
原文地址:http://scottwater.com/articles/ScrollPage
感谢scottwater先生!
我做了一点修改,使其支持横向滚动条位置的记忆
Disclaimer#region Disclaimer
/**//**********************************************************************
Based on presentation by Brad McCabe of Infragistics
Updated by Scott Watermasysk (http://scottwater.com)
Provided as is, with no warrenty, etc.
Please use however you see fit. Just don't ask for a VB version :)
***********************************************************************/
#endregion
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"]));
}
}
}
}
/**//**********************************************************************
Based on presentation by Brad McCabe of Infragistics
Updated by Scott Watermasysk (http://scottwater.com)
Provided as is, with no warrenty, etc.
Please use however you see fit. Just don't ask for a VB version :)
***********************************************************************/
#endregion
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"]));
}
}
}
}
- Scroll Page 表单提交后页面重新滚回原来滚动条所在位置
- Scroll Page 表单提交后页面重新滚回原来滚动条所在位置
- Scroll Page 表单提交后页面重新滚回原来滚动条所在位置
- 保持页面中滚动条的位置,使得页面提交后滚动条还是在原来的位置
- jquery(js)判断页面滚动条(scroll)是向上滚还是向下滚方向判断
- 页面刷新后滚动条重新定位
- 点击提交后滚动条还是在原来的位置
- 页面刷新后跳滚动条保持原来的位置
- 页面滚动条 刷新后 保持在原来的位置
- jquery判断页面滚动条(scroll)是上滚还是下滚,且是否滚动到头部或者底部
- jquery判断页面滚动条(scroll)是上滚还是下滚,且是否滚动到头部或者底部
- jquery判断页面滚动条(scroll)是上滚还是下滚,且是否滚动到头部或者底部
- 怎么实现表单提交后不重新刷新当前页面
- 怎么实现表单提交后不重新刷新当前页面
- 怎么实现表单提交后不重新刷新当前页面
- 实现提交页面后滚动条自动移动问题
- js实现表单提交后不重新刷新当前页面,并停留在当前页面
- jqgrid标题换行、隐藏滚动条、重新提交数据加载页面、单元格绑定点击事件
- [转]企业信息管理系统 用户需求报告编写指南
- 常用开源协议解析
- 成功的部门经理一周速成(转载)
- IntelliJ IDEA使用技巧一览表(转)
- 如何卸载NORTON杀毒软件-MITAC企业版
- Scroll Page 表单提交后页面重新滚回原来滚动条所在位置
- 初来乍到
- ASP代码变成组件 加快ASP的速度(转贴)
- 理性和意识的关系
- R/3常用事务代码表
- [转]不易破解的软件注册码算法(By 笑望人生)
- 工作在压力下
- [转帖]变速齿轮的一种实现方法(内有中断门的创建与调用)
- 向高手请教一个问题?