DropDownList的不触发onb​​eforeunload时的AutoPostBack ="真" [英] DropDownList not firing onbeforeunload when AutoPostBack="True"

查看:125
本文介绍了DropDownList的不触发onb​​eforeunload时的AutoPostBack ="真"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了使用在这些网页上描述的技术是未保存的更改警告:

http://stackoverflow.com/questions/140460

http://kenbrowning.blogspot.com/2009/ 01 /使用-的jQuery到standardize.html

这工作得很好,除了在页面上一个DropDownList。它的的AutoPostBack,我想onbeforeunload火因未保存的更改将丢失,但它不工作。它应该是提高onbeforeunload事件?我能以某种方式使其引发事件?

编辑:
将DropDownList是一个UpdatePanel里面,所以这意味着它不卸载页面,这将是为什么onbeforeunload不被触发。有没有什么办法可以编程触发事件?还是我必须推出自己的仿制确认对话框?

EDIT2
我现在有一个解决方案,从一个UpdatePanel添加对话框异步回发。我已经编辑原始脚本,将调用在我的解决方案描述setConfirmAsyncPostBack()。

下面是我的javascript:

  / ****脚本来警告未保存的更改用户**** ///http://stackoverflow.com/questions/140460
//http://jonstjohn.com/node/23//激活确认消息onbeforeunload。
功能setConfirmUnload(上){    setConfirmAsyncPostBack();    如果(上){
        removeCheckFromNoWarnClasses();
        fixIEonBeforeUnload();
        window.onbeforeunload = unloadMessage
        返回;
    }    window.onbeforeunload = NULL
}功能unloadMessage(){    返回你有未保存的更改。';
}// JavaScript的移动从HREF到的onclick至prevent IE提高onbeforeunload unecessarily
//http://kenbrowning.blogspot.com/2009/01/using-jquery-to-standardize.html
功能fixIEonBeforeUnload(){    如果(!$。browser.msie)
        返回;
    $('一个')。过滤(功能(){
        返回(/^javascript\\:/i).test($(this).attr('href'));
    })。每个(函数(){
        VAR hrefscript = $(本).attr('href属性);
        hrefscript = hrefscript.substr(11);
        $(本)。数据(hrefscript',hrefscript);
    })。点击(函数(){
        VAR hrefscript = $(本)。数据(hrefscript');
        的eval(hrefscript);
        返回false;
    })ATTR('的href','#');
}//移除保存按钮,链接等,已经可以得到无警告或无警告 - 验证CSS类的警告
//无警告 - 验证的输入/链接将仅删除验证成功后警告
//使用无警告,校验类上的按钮,导致验证/链接。
//使用无警告级上有控制的CausesValidation =假(例如一个另存为草稿按钮)。
功能removeCheckFromNoWarnClasses(){    $('。没有警告-验证')。点击(函数(){
        如果(Page_ClientValidate == NULL || Page_ClientValidate()){
            setConfirmUnload(假);
        }
    });    $('没有警告')。点击(函数(){
        setConfirmUnload(假);
    });
}//添加客户端事件的所有输入控件在确认onbeforeunload切换
功能enableUnsavedChangesWarning(){    $(':输入')一('变',函数(){。
        window.onbeforeunload =功能(){
            返回你有未保存的更改。';
        }
    });    removeCheckFromNoWarnClasses();
}

在我的ASP.NET页面,当用户进行了更改:

 如果(改变)
    {
        ...
        //确认卸载,如果有未保存的更改。
        //注意我们也有叫fixIEonBeforeUnload()来修复链接,在网页加载中完成的,包括那些回调期间呈现链接
        ScriptManager.RegisterStartupScript(页,的GetType(),unsavedchanges,setConfirmUnload(真);,真);
    }
    其他
        ...


解决方案

另请参阅<一个href=\"http://stackoverflow.com/questions/3298140/how-to-$p$pvent-autopostback-when-dropdownlist-is-selected-using-jquery/3299334#3299334\">http://stackoverflow.com/questions/3298140/how-to-$p$pvent-autopostback-when-dropdownlist-is-selected-using-jquery/3299334#3299334

  //http://msdn.microsoft.com/en-us/magazine/cc163413.aspx
//http://stackoverflow.com/questions/2424327/$p$pvent-asp-net-dopostback-from-jquery-submit-within-updatepanel
//添加事件处理函数时异步回发是由一个UpdatePanel初始化确认未保存的更改
功能setConfirmAsyncPostBack(){    如果(typeof运算(Sys.WebForms)===未定义|| typeof运算(Sys.WebForms.PageRequestManager)===未定义)
        返回;    变种PRM = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(confirmAsyncPostBack);
}//事件处理异步回发,如果他们不确认,确认未保存的更改,取消回发
//添加的确认有一个CSS类元素的警告
功能confirmAsyncPostBack(发件人,参数){
    如果(window.onbeforeunload = NULL&放大器;!&安培; args.get_postBackElement()类名==警告&放大器;&安培;!unloadConfirmed())
        args.set_cancel(真);
}//显示模仿由onbeforeunload显示的对话框确认对话框
功能unloadConfirmed(){    VAR确认=确认(你确定要导航离开此页面吗?\\ n \\ n+ unloadMessage()+\\ n \\ n preSS确定继续或取消停留在当前页面。 );
    如果(确认)
        window.onbeforeunload = NULL;
    返回确认;
}

I have implemented an "unsaved changes" warning using techniques described on these pages:

http://stackoverflow.com/questions/140460

http://kenbrowning.blogspot.com/2009/01/using-jquery-to-standardize.html

This works well except for a DropDownList on the page. It does an AutoPostBack, and I want onbeforeunload to fire because unsaved changes will be lost, but it isn't working. Should it be raising the onbeforeunload event? Can I somehow make it raise the event?

Edit: The DropDownList is inside an UpdatePanel, so that means it isn't unloading the page and that would be why onbeforeunload isn't being triggered. Is there any way I can trigger the event programmatically? Or do I have to roll my own imitation Confirm dialog?

Edit2 I now have a solution that adds the dialog to asynchronous postbacks from an UpdatePanel. I have edited the original script, adding the call to setConfirmAsyncPostBack() as described in my solution.

Here is my javascript:

/****Scripts to warn user of unsaved changes****/

//http://stackoverflow.com/questions/140460
//http://jonstjohn.com/node/23

//Activates the confirm message onbeforeunload.
function setConfirmUnload(on) {

    setConfirmAsyncPostBack();

    if (on) {
        removeCheckFromNoWarnClasses();
        fixIEonBeforeUnload();
        window.onbeforeunload = unloadMessage
        return;
    }

    window.onbeforeunload = null
}

function unloadMessage() {

    return 'You have unsaved changes.';
}

//Moves javascript from href to onclick to prevent IE raising onbeforeunload unecessarily
//http://kenbrowning.blogspot.com/2009/01/using-jquery-to-standardize.html
function fixIEonBeforeUnload() {

    if (!$.browser.msie)
        return;
    $('a').filter(function() {
        return (/^javascript\:/i).test($(this).attr('href'));
    }).each(function() {
        var hrefscript = $(this).attr('href');
        hrefscript = hrefscript.substr(11);
        $(this).data('hrefscript', hrefscript);
    }).click(function() {
        var hrefscript = $(this).data('hrefscript');
        eval(hrefscript);
        return false;
    }).attr('href', '#');
}

//Removes warnings from Save buttons, links, etc, that have been can be given "no-warn" or "no-warn-validate" css class
//"no-warn-validate" inputs/links will only remove warning after successful validation
//use the no-warn-validate class on buttons/links that cause validation. 
//use the no-warn class on controls that have CausesValidation=false (e.g. a "Save as Draft" button).
function removeCheckFromNoWarnClasses() {

    $('.no-warn-validate').click(function() {
        if (Page_ClientValidate == null || Page_ClientValidate()) {
            setConfirmUnload(false);
        }
    });

    $('.no-warn').click(function() {
        setConfirmUnload(false);
    });
}

//Adds client side events to all input controls to switch on confirmation onbeforeunload
function enableUnsavedChangesWarning() {

    $(':input').one('change', function() {
        window.onbeforeunload = function() {
            return 'You have unsaved changes.';
        }
    });

    removeCheckFromNoWarnClasses();
}

And in my ASP.NET page, when the user makes a change:

    if (changed)
    {
        ...
        //Confirm unload if there are unsaved changes. 
        //NB we also have to call fixIEonBeforeUnload() to fix links, done in in page load to include links that are rendered during callbacks
        ScriptManager.RegisterStartupScript(Page, GetType(), "unsavedchanges", "setConfirmUnload(true);", true);
    }
    else
        ...

解决方案

Also see http://stackoverflow.com/questions/3298140/how-to-prevent-autopostback-when-dropdownlist-is-selected-using-jquery/3299334#3299334

//http://msdn.microsoft.com/en-us/magazine/cc163413.aspx
//http://stackoverflow.com/questions/2424327/prevent-asp-net-dopostback-from-jquery-submit-within-updatepanel
//Adds an event handler to confirm unsaved changes when an asynchronous postback is initialised by an UpdatePanel
function setConfirmAsyncPostBack() {

    if (typeof (Sys.WebForms) === "undefined" || typeof (Sys.WebForms.PageRequestManager) === "undefined")
        return;

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(confirmAsyncPostBack);
}

//An event handler for asynchronous postbacks that confirms unsaved changes, cancelling the postback if they are not confirmed
//Adds the confirmation to elements that have a css class of "warn"
function confirmAsyncPostBack(sender, args) {
    if (window.onbeforeunload != null && args.get_postBackElement().className == "warn" && !unloadConfirmed())
        args.set_cancel(true);
}

//Displays a confirmation dialog that imitates the dialog displayed by onbeforeunload
function unloadConfirmed() {

    var confirmed = confirm("Are you sure you want to navigate away from this page?\n\n" + unloadMessage() + "\n\nPress OK to continue or Cancel to stay on the current page.");
    if (confirmed)
        window.onbeforeunload = null;
    return confirmed;
}

这篇关于DropDownList的不触发onb​​eforeunload时的AutoPostBack =&QUOT;真&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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