ASP.Net页面刷新后自动滚动到原来位置
2024-08-20 12:22:34
在网上搜索之后总结了三种方式:
1.设置Page中的MaintainScrollPositionOnPostback属性为true
A>.页面里有MaintainScrollPositionOnPostback,默认是false,设为true即可(页面级)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" MaintainScrollPositionOnPostback="true" Inherits="Default.Defa
ult" %>
B>.设置web.config配置文件中的Pages节点中的MaintainScrollPositionOnPostback属性为true(网站级或目录级)
如果修改网站根目录下的web.config则所有的页面均会受影响,若只修改某个目录下的web.config文件则只影响这个目录中的页面
具体办法:
在<system.web>节点下配置:
<pages maintainScrollPositionOnPostBack="true"></pages>
C>.在页面的code页面,通过C#或VB代码设置page的MaintainScrollPositionOnPostback属性为true
Page.MaintainScrollPositionOnPostBack = true;
或这样写
this.MaintainScrollPositionOnPostBack = true;
2.可以使用Jquery,通过获取某个元素当前位置的高度,具体实现如下
function setPosition()
{
var top=$("#元素id").offset().top();
$("html,body").animate({scrollTop:top},1000);
}
3.可以使用锚点,但这里可使用灵活处理
首先获取需要滚动到的位置的id,如,可以设置一个元素(<span name="postion" id="postion"></span>,注:要在form里),另外在form的任意位置设置
<a href="#postion" id="clickLink"></a>
注:a标签里不要有内容,在回传的地方调用
Page.ClientScript.RegisterStartupScript(this.GetType(), "scroll", "document.getElementById('clickLink').click();", true);
这种方法其实就是触发某个元素的事件
最新文章
- Java学习笔记(七)&mdash;&mdash;对象
- http://blog.sina.com.cn/s/blog_5bd6b4510101585x.html
- Oracle常用监控SQL
- ahjesus如何在windows下制作适用于mac的u盘启动盘
- android studio annotation 配置过程
- Spring day01
- git学习笔记12-标签管理-版本
- 铁人系列(2)LA2218
- poj2528(线段树+离散化)Mayor&#39;s posters
- 【转】Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- ubuntu 14.04 apt-get 方式安装oracle JDK
- mac版sublime text2包管理器安装步骤
- 类型“System.Data.Objects.DataClasses.EntityObject”在未被引用的程序集中定义。
- 消除警告";property access result unused - getters should not be used for side effects";
- Carbondata源码系列(一)文件生成过程
- Python面向对象:杂七杂八的知识点
- Mysql中设置指定IP的特定用户及特定权限
- <;转载>;ford-fulkerson算法
- java子类继承关系
- Web重温系列(二):SQLite+EF6实现本地化存储