ASP.NET,jQuery的,肮脏的形式,window.onbeforeunload [英] ASP.NET, jQuery, dirty forms, and window.onbeforeunload

查看:230
本文介绍了ASP.NET,jQuery的,肮脏的形式,window.onbeforeunload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.NET Web应用程序,我试图创建一个从形式离开航行时,他们已经进行了更改,使用jQuery前警告用户的普遍方式。 pretty标准的东西,但很多搜索我还没有找到一个可行的技术之后。

In my ASP.NET web app, I'm trying to create a universal way of warning users before navigating away from a form when they've made changes, using jQuery. Pretty standard stuff, but after a lot of searching I have yet to find a technique that works.

下面是我在这一点:

    addToPostBack = function(func) {
        var old__doPostBack = __doPostBack;
        if (typeof __doPostBack != 'function') {
            __doPostBack = func;
        } else {
            __doPostBack = function() {
                old__doPostBack();
                func();
            }
        }
    }

    var isDirty = false;

    $(document).ready(function() {
        addToPostBack(function() {
            alert("Postback detected.")
            clearDirty();
        });
        $(':input').bind("change select keydown", setDirty);
        window.onbeforeunload = function(e) {
            var msg = "You have unsaved changes. "
            if (isDirty == true) {
                var e = e || window.event;
                if (e) { e.returnValue = msg; }
                return msg;
            }
        };
    });

    setDirty = function() {isDirty = true;}

    clearDirty = function() {isDirty = false;}

这工作就从警告离开页面的用户。问题是,我得到的每同一页回发警告。有一些关于我的事情形式可能会触发回发:

This works as far as warning the user from navigating away. The problem is that I get the warning on every same-page postback. There are a number of things on my forms that might trigger a postback:


  1. 有保存,取消,并在页面上删除linkbuttons

  2. 有可能是页面上的其他linkbuttons的执行服务器端功能,同时在同一页
  3. 在留
  4. 有可能是与自动回发其他控件=真也附加有服务器端的功能,但不导致在用户离开网页。

这些事情应该产生一个警告,因为用户不离开页面无。我的code试图劫持addToPostBack(<一个href=\"http://stackoverflow.com/questions/1230573/how-to-capture-submit-event-using-jquery-in-an-asp-net-application/1234845\">more就在这个问题)的详细信息以清除isDirty位回发过,但问题是,在IE onbeforeunload __doPostBack前大火,显然是因为IE触发onb​​eforeunload时立即点击一个链接(<一个href=\"http://stackoverflow.com/questions/317423/asp-net-linkbutton-raising-onbeforeunload-event-twice\">as这里描述)。

None of these things should provoke a warning, because the user isn't leaving the page. My code tries to hijack addToPostBack (more details on that in this question) to clear the isDirty bit before posting back, but the problem is that in IE onbeforeunload fires before __doPostBack, apparently because IE fires onbeforeunload immediately when a link is clicked (as described here).

当然,我可以连线了每个这些控件以清除isDirty位,但我preFER上形式层面操作的解决方案,并且不要求我接触的每一个控件可能触发回发。

Of course, I could wire up each of these controls to clear the isDirty bit, but I'd prefer a solution that operates on the form level and that doesn't require that I touch every single control that might trigger a postback.

有没有人有在ASP.NET工作的方法,并且不涉及布线每隔可能导致回发的控制?

Does anyone have an approach that works in ASP.NET and that doesn't involve wiring up every control that might cause a postback?

推荐答案

我碰到这个职位来到谷歌搜索的同时为MVC中做同样的事情的解决方案。该解决方案,改编自香草的上面,似乎运作良好。由于没有什么MVC-具体谈谈这一点,它应该工作一样好了PHP,ASP经典,或任何其他类型的应用程序使用HTML和JQuery。

I came across this post while Googling for a solution for doing the same thing in MVC. This solution, adapted from Herb's above, seems to work well. Since there's nothing MVC-specific about this, it should work just as well for PHP, Classic ASP, or any other kind of application that uses HTML and JQuery.

var isDirty = false;

$(document).ready(function () {
    $(':input').bind("change select keydown", setDirty);
    $('form').submit(clearDirty);

    window.onbeforeunload = function (e) {
        var msg = "You have unsaved changes. "
        if (isDirty == true) {
            var e = e || window.event;
            if (e) { e.returnValue = msg; }
            return msg;
        }
    };
});

setDirty = function () { isDirty = true; }
clearDirty = function () { isDirty = false; }

这篇关于ASP.NET,jQuery的,肮脏的形式,window.onbeforeunload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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