Javascript 在asp.net中回发完成时如何滚动到页面底部?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1911820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 21:52:27  来源:igfitidea点击:

How to scroll to bottom of page when postback finish in asp.net?

asp.netjavascript

提问by monkey_boys

How to scroll to bottom of page when postback finish in asp.net?

在asp.net中回发完成时如何滚动到页面底部?

I have many details in page when I click "Show Detail" in master detail, this page show many data in my page. So how to to scroll to bottom of page automatically?

当我在主详细信息中单击“显示详细信息”时,页面中有很多详细信息,此页面在我的页面中显示了许多数据。那么如何自动滚动到页面底部呢?

回答by ram

from Hosam Kamel's page

来自Hosam Kamel 的页面

To maintain the scroll position for the large web page you can use on of these methods :

要保持大网页的滚动位置,您可以使用以下方法之一:

1- use Web.config page section <pages maintainScrollPositionOnPostBack="true" />

1-使用Web.config页面部分 <pages maintainScrollPositionOnPostBack="true" />

: this will maintains the scroll positions for all the web site pages.

:这将保持所有网站页面的滚动位置。

2- in the page declaration <%@ Page MaintainScrollPositionOnPostback="true" %>: this will maintains the scroll position for this page only.

2- 在页面声明中 <%@ Page MaintainScrollPositionOnPostback="true" %>:这将仅保持该页面的滚动位置。

3- programmatically from code behind System.Web.UI.Page.MaintainScrollPositionOnPostBack = true;: this will maintains the scroll position for this page only (the same as page declration).

3- 从后面的代码中以编程方式System.Web.UI.Page.MaintainScrollPositionOnPostBack = true;:这将仅维护此页面的滚动位置(与页面声明相同)。

回答by Edgar Hernandez

You could register the a javascript to move the scroll to the position of some control that you want, like this:

您可以注册一个 javascript 以将滚动条移动到您想要的某个控件的位置,如下所示:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            RegisterStartupScript("ScrollScript", "document.getElementById('objectId').scrollIntoView(true);");
        }
    }

Changing the objectId in the script for the Id of the object you want to scroll to.

将脚本中的 objectId 更改为要滚动到的对象的 Id。

As noted by Guy Starbuk in the comments, RegisterStartupScriptis deprecated in .Net 4 and now the recommended way to register a script is:

正如 Guy Starbuk 在评论中所指出的,RegisterStartupScript在 .Net 4 中已被弃用,现在推荐的注册脚本的方法是:

 ClientScript.RegisterStartupScript(GetType(), "ScrollScript", "document.getElementById('objectId').scrollIntoVie??w(true)", true);

回答by Ernesto Rodriguez Lucas

In asp.net web pages you can add an OnClientClick event to the control causing the server post back to scroll the page to the bottom.

在 asp.net 网页中,您可以向控件添加 OnClientClick 事件,从而导致服务器回发将页面滚动到底部。

<asp:Button ID="MyButton" runat="server" Text="Submit" OnClick="MyButton_Click" OnClientClick="window.scrollTo(0, document.body.scrollHeight);" />

回答by MikeW

Create an anchor on the page, then on onload:

在页面上创建一个锚点,然后在 onload 上:

window.location.href = "#myanchor"

回答by Tod

One thing missing from the answers here is a delay. It's all well and good if there is no change in the height of the web page during load. But if there are a lot of images and your trying scroll past them; they can bump your view back up again.

这里的答案中缺少的一件事是延迟。如果在加载过程中网页的高度没有变化,那一切都很好。但是如果有很多图像并且您尝试滚动它们;他们可以再次让您的视野恢复原状。

What is needed is a window.onload function call:

需要的是 window.onload 函数调用:

ClientScript.RegisterStartupScript(GetType(), "ScrollScript", "window.onload = function() {document.getElementById('objectid').scrollIntoView(true);}", true);