asp.net – 如何避免UpdatePanel在AutoPostBack上滚动?

我在一个更新面板中有一个ASP.NET FormView.我通过为FormView中的每个项目设置AutopostBack = true来自动保存表单.

这意味着用户可以快速点击几个元素,几乎同时点击几个异步回发.

我的问题是用户能够在异步回发尚未完成的同时继续向下滚动窗体.浏览器总是滚动回到它在第一次回发的位置.

Page.MaintainScrollPositionOnPostback设置为False.

我在ajax和jquery中尝试过各种各样的事情:

> pageLoad
> add_initializeRequest
> add_endRequest
> document.ready
>等..

但是我总是只能像第一次回发一样访问滚动Y.

当回发完成时,是否有任何方法来检索当前的滚动Y,所以我可以停止滚动发生?或者是否可以禁用滚动行为?

谢谢!

更新

感谢@chprpipr,我得到了这个工作.这是我的缩写解决方案:

var FormScrollerProto = function () {
    var Me = this;
    this.lastScrollPos = 0;
    var myLogger;

    this.Setup = function (logger) {
        myLogger = logger;
        // Bind a function to the window
        $(window).bind("scroll",function () {
            // Record the scroll position
            Me.lastScrollPos = Me.GetScrollTop();
            myLogger.Log("last: " + Me.lastScrollPos);
        });
    }

    this.ScrollForm = function () {
        // Apply the last scroll position
        $(window).scrollTop(Me.lastScrollPos);
    }

    // Call this in pageRequestManager.EndRequest
    this.EndRequestHandler = function (args) {
        myLogger.Log(args.get_error());
        if (args.get_error() == undefined) {
            Me.ScrollForm();
        }
    }

    this.GetScrollTop = function () {
        return Me.FilterResults(
                window.pageYOffset ? window.pageYOffset : 0,document.documentElement ? document.documentElement.scrollTop : 0,document.body ? document.body.scrollTop : 0
            );
    }

    this.FilterResults = function (n_win,n_docel,n_body) {
        var n_result = n_win ? n_win : 0;
        if (n_docel && (!n_result || (n_result > n_docel)))
            n_result = n_docel;
        return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
    }
}

主页:

...snip...

var logger;
    var FormScroller;

    // Hook up Application event handlers.
    var app = Sys.Application;

    // app.add_load(ApplicationLoad); - use pageLoad instead
    app.add_init(ApplicationInit);
    // app.add_disposing(Applicationdisposing);
    // app.add_unload(ApplicationUnload);

    // Application event handlers for component developers.
    function ApplicationInit(sender) {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        if (!prm.get_isInAsyncPostBack()) {
            prm.add_initializeRequest(InitializeRequest);
            prm.add_beginRequest(BeginRequest);
            prm.add_pageLoading(PageLoading);
            prm.add_pageLoaded(PageLoaded);
            prm.add_endRequest(EndRequest);
        }

        // Set up components
        logger = new LoggerProto();
        logger.Init(true);
        logger.Log("APP:: Application init.");

        FormScroller = new FormScrollerProto();
    }

    function InitializeRequest(sender,args) {
        logger.Log("PRM:: Initializing async request.");
        FormScroller.Setup(logger);
    }

...snip...

function EndRequest(sender,args) {
        logger.Log("PRM:: End of async request.");

        maintainScroll(sender,args);

        // display any errors
        processErrors(args);
    }

...snip...

function maintainScroll(sender,args) {
        logger.Log("maintain: " + winScrollTop);
        FormScroller.EndRequestHandler(args);
    }

我也尝试调用EndRequestHandler(必须删除args.error检查),看看它是否减少了滚动时闪烁,但它没有.值得注意的是,完美的解决方案是停止浏览器尝试滚动,现在有一个暂时的抖动,这在用户群庞大的应用程序中是不可接受的.

(滚动顶部的代码不是我的 – 在网上发现)

(这是客户端生命周期中有用的MSDN页面http://msdn.microsoft.com/en-us/library/bb386417.aspx)

3月7日更新:

我刚刚发现一个非常简单的方式来做到这一点:

<script type="text/javascript">

var prm = Sys.WebForms.PageRequestManager.getInstance();

prm.add_beginRequest(beginRequest);

function beginRequest()
{
    prm._scrollPosition = null;
}

</script>

解决方法

您可以绑定一个记录当前滚动位置的函数,然后在每个endRequest之后重新应用它.它可能会像这样:
// Wrap everything up for tidiness' sake
var FormHandlerProto = function() {
    var Me = this;

    this.lastScrollPos = 0;

    this.SetupForm = function() {
        // Bind a function to the form's scroll container
        $("#ContainerId").bind("scroll",function() {
            // Record the scroll position
            Me.lastScrollPos = $(this).scrollTop();
        });
    }

    this.ScrollForm = function() {
        // Apply the last scroll position
        $("#ContainerId").scrollTop(Me.lastScrollPos);
    }

    this.EndRequestHandler = function(sender,args) {
        if (args.get_error() != undefined)
            Me.ScrollForm();
        }
    }
}

var FormHandler = new FormHandlerProto();
FormHandler.Setup(); // This assumes your scroll container doesn't get updated on postback.  If it does,you'll want to call it in the EndRequestHandler.

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(FormHandler.EndRequestHandler);

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...