异步回发后重置滚动位置 - ASP.NET [英] Reset scroll position after Async postback - ASP.NET

查看:21
本文介绍了异步回发后重置滚动位置 - 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 列启动,在 GridView OnRowCommand 中调用 ASP.NET 更新面板 Update 方法.

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 网站.

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:

如何将其配置为仅在用户单击 GridView 控件中的 ASP.NET CommandField 时执行?我在页面上有其他按钮执行异步回发,我不想滚动到页面顶部.

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.

推荐答案

这是我基于这个来源

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 Webform 代码

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();");
            }
        }
    }
}

这篇关于异步回发后重置滚动位置 - ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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