重置滚动位置异步回发后 - ASP.NET

问题描述:

什么是重置滚动位置,页面异步回发后,顶部的最佳方法?

What is the best way to reset the scroll position to the top of page after the an asynchronous postback?

异步回发是从ASP.NET的GridView CommandField中柱发起和ASP.NET更新面板调用Update方法在GridView OnRowCommand。

The asynchronous postback is initiated from a ASP.NET GridView CommandField column and the ASP.NET update panel Update method is called in the GridView OnRowCommand.

我现在的应用程序是一个ASP.NET 3.5 Web站点。

My current application is an ASP.NET 3.5 web site.

编辑:我收到了大家极好的反馈,我用PageRequestManager方法的脚本标记结束了,但我的下一个问题是:

I have received excellent feedback from everyone, and I ended using the PageRequestManager method in a script tag, but my next question is:

我如何配置它,只有当用户点击ASP.NET CommandField中在我的GridView控件执行?我在网页上执行,我不想滚动到页面顶部的异步回送其他按钮。

How do I configure it to only execute when the user clicks the ASP.NET CommandField in my GridView control? I have other buttons on the page that perform an asynchronous postback that I do not want to scroll to the top of the page.

编辑1:我已经开发了一个解决方案,我并不需要使用PageRequestManager。请参阅我的解决办法跟进的答案。

EDIT 1: I have developed a solution where I do not need to use the PageRequestManager. See my follow up answer for solution.

下面是以下解决方案我开发在此基础上的来源

Here is the following solution I developed based on this source

ASP.NET网络表单

ASP.NET Webform

<script language="javascript" type="text/javascript">
   function SetScrollEvent() {
      window.scrollTo(0,0);
   }
</script> 

<asp:GridView id="MyGridView" runat="server" OnRowDataBound="MyGridView_OnRowDataBound">
    <Columns>
        <asp:CommandField ButtonType="Link" ShowEditButton="true" />
    </Columns>
</asp:GridView>

ASP.NET网络表单$ C $后面

ASP.NET Webform code behind

protected void MyGridView_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType.Equals(DataControlRowType.DataRow))
    {
        foreach (DataControlFieldCell cell in e.Row.Cells)
        {
            foreach(Control control in cell.Controls)
            {
                LinkButton lb = control as LinkButton;

                if (lb != null && lb.CommandName == "Edit")
                    lb.Attributes.Add("onclick", "SetScrollEvent();");
            }
        }
    }
}