回发后防止页面滚动并保持位置 [英] Prevent page scrolling after postback and maintain position

查看:67
本文介绍了回发后防止页面滚动并保持位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据用户在CheckBoxList中的检查来加总用户的分数.每次用户选中一个框时,都会在总分中添加一个值X.当用户取消选中一个复选框时,将从总分中减去值X.没问题.

I am working with adding up user's scores based on their checks in a CheckBoxList. Every time a user checks a box, a value, X, is added to the overall score. When a user unchecks a box, a value, X, is subtracted from the overall score. No problems here.

我遇到的问题是,在CheckBoxList属性中使用AutoPostback选项会迫使页面重新加载到顶部,而不是停留在用户所在的位置,这意味着在每次检查/之后,他们必须继续向下滚动取消选中.有办法防止这种情况吗?

The problem that I am having is that using the AutoPostback option in the CheckBoxList properties forces the page to load back to the top instead of staying where the user was situated, which means that they have to keep scrolling down after each check/uncheck. Is there a way to prevent this?

推荐答案

Ajax解决方案

当然,最好的方法是对此使用Ajax调用.页面根本不移动,而数据仅被更新. updatepanel是一种快速简便的启动解决方案-并非最佳解决方案,但是如果您的页面简单,那将是一个很好的解决方案.

Ajax solution

Of course, the best way is to use an Ajax call on that. The page is not moved at all, and the data is just updated. The updatepanel is a fast and easy solution for starting - not an optimal solution, but if you have a simple page, it is a very good one.

第二种解决方案是使用锚点号.您可以设置显示点:

A second solution is to use anchor #. You set the point on which you like it to show up:

<a name="PointA"></a>

然后使用该锚点作为page.aspx#PointA来调用页面.

And you call the page using that anchor as page.aspx#PointA.

第三个解决方案是使用ASP.NET的内部JavaScript代码.在页面声明(第一行的顶部)上.<%@ Page MaintainScrollPositionOnPostback="true" %>.

A third solution is to use the inside JavaScript code of ASP.NET. On the page declaration (top first line) <%@ Page MaintainScrollPositionOnPostback="true" %>.

或在web.config上影响所有页面,<pages maintainScrollPositionOnPostBack="true" />.

Or on the web.config to affect all pages, <pages maintainScrollPositionOnPostBack="true" />.

或通过编程方式System.Web.UI.Page.MaintainScrollPositionOnPostBack = true;打开并根据需要将其关闭.

Or programmatically System.Web.UI.Page.MaintainScrollPositionOnPostBack = true; to open it and close it by demand.

仅用两行jQuery代码,您就可以在发回帖子后在要移动的点上制作漂亮的动画:

With only two lines of jQuery code you can make a nice animation on the point you like to move after the post back:

var WhereToMove = jQuery("#PointA").position().top;
jQuery("html,body").animate({scrollTop: WhereToMove }, 1000);

然后将页面移至该元素:

And you move the page to this element:

<a id="PointA" name="PointA"></a>

Google搜索

最后,您可以使用自定义JavaScript代码执行相同的操作.互联网上有许多用于此目的的示例: https://www.google. com/?q = asp.net + remain + position

这篇关于回发后防止页面滚动并保持位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆