ASP.NET客户端脏表单处理 [英] ASP.NET Client-Side Dirty-Form Handling

查看:112
本文介绍了ASP.NET客户端脏表单处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到最佳方法来处理此问题,这是在提醒ASP.NET Web表单用户注意由于以下原因而可能造成的数据丢失:

I've been trying to find the best way to handle this, this being alerting the user of an ASP.NET web form to the impending loss of data due to either:

-通过链接更改表单, -点击返回", -关闭浏览器

-Changing forms via a link click, -Hitting "Back", -Closing the browser

我希望他们这样做时出现一个简单的对话框,让他们可以改变主意.如果他们这样做,则保留表格.如果没有,继续.

I want a simple dialog to appear when they do so, giving them the option to change their minds. If they do, then the form remains. If they don't, carry on.

我已经尝试了几个jQuery插件来解决这个问题,并且目前在dirty_form上.当我单击链接或尝试关闭窗口时,它会正确地捕获肮脏的表单并提醒我(它不会向后退).

I've tried several jQuery plugins to handle this and am currently on dirty_form. It is properly trapping dirty forms and alerting me when I click a link or try to close the window (it doesn't handle back).

不幸的是,处理脏表单情况还不够,因为我似乎无法使其做任何事情.我的代码:

Unfortunately, handling the dirty form situation isn't enough as I cant seem to make it DO anything. My code:

<script type="text/javascript">
    $(document).ready(function () {
        $("#form2").dirty_form().dirty(function (event, data) {
            /*what here */
        });
    });
</script>

如何设置此项,以便他们选择前进,如果不选择前进,则不行?

How do I set this so that if they choose to go forward, it does and if they don't, it doesn't?

此外,如何捕获浏览器的后退"功能并以相同的方式处理它?<​​/p>

Also, how to I trap the browser "Back" function and handle it in the same way?

推荐答案

Following the answer at this question here on SO, I implemented this dirty_form plugin successfully in our app. This is what we ended up doing:

// A global variable to table any existing onbeforeunload event
var oldOnBeforeUnloadEvent;

// onDirty event that gets triggered by the plugin.. add visual cue to the user to show dirtiness 
function onDirty(event, data) {
    $("#form2 div.dirtyIndicator").addClass("redAsteriskBG");
}

// onClean event that gets triggered by the plugin.. remove the dirty indicator
function onClean(event, data) {
    $("#form2 div.dirtyIndicator").removeClass("redAsteriskBG");
}

// Check for presence of any inputs that are changed
function checkForDirtyElements() {
    var message = '';
    var dirtyElements = $('.myChangedClassName').length;
    if (dirtyElements > 0) {
        message += "IF YOU CONTINUE, ALL YOUR CHANGES WOULD BE LOST!";
    }
    return message;
}

// Our onbeforeunload event that does all the magic
function myOnBeforeUnload() {
    var message = checkForDirtyElements();
    if (message === '') {
        if (oldOnBeforeUnloadEvent !== null) {
            message = oldOnBeforeUnloadEvent();
        }
    }
    if (message.length > 0) {
        return message;
    }
}

$(document).ready(function () {
    // On page load set-up/initialize the dirty form with onDirty and onClean events
    $("#form2").dirty_form({
        changedClass: 'myChangedClassName'
    }).dirty(onDirty).clean(onClean);

    // Save the existing onbeforeunload event to a global variable and attach our custom onbeforeunload
    oldOnBeforeUnloadEvent = window.onbeforeunload;
    window.onbeforeunload = myOnBeforeUnload;
});

这篇关于ASP.NET客户端脏表单处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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