当使用jQuery选择DROPDOWNLIST如何prevent的AutoPostBack [英] How to prevent AutoPostBack when DropDownlist is selected using jQuery

查看:193
本文介绍了当使用jQuery选择DROPDOWNLIST如何prevent的AutoPostBack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当用户在DropDownList中选择一个项目显示一个确认对话框。如果用户presses取消,我想停止回发。下面是我添加到onchange事件功能:

I want to show a confirm dialog when the user selects an item in a DropDownList. If the user presses "Cancel", I want to stop the postback. Here is the function I add to the onchange event:

function imitateConfirmUnload(event) {
    if (window.onbeforeunload = null)
        return true;

    return confirm("Are you sure you want to navigate away from this page?\n\nYou have unsaved changes\n\nPress OK to continue or Cancel to stay on the current page.");
}

这是code在我的启动脚本中的相关位的处理程序添加到事件:

And this is the relevant bit of code in my startup script to add the handler to the event:

function addConfirmToWarnClasses() {

    $('.warn').change(function() {
        imitateConfirmUnload()
    });
}

的问题是,即使用户选择了取消发生回发。如果我移动处理器上单击事件,它的工作原理。但感觉笨重给我。

The problem is that the postback occurs even if the user selects "Cancel". If I move the handler on to the click event, it works. But it feels clunky to me.

修改

修正:它没有的onclick工作,因为对话prevents选择,所以当用户选择OK,没有任何变化已经发生,而当你想它没有回发

Correction: it doesn't work onclick, because the dialog prevents selection, so when the user selects OK, no change has taken place, and no postback when you want it!

编辑2

DropDownList控件是一个UpdatePanel内,这样会影响行为。

The DropDownList is inside an UpdatePanel so that may affect behaviour.

推荐答案

果然,从更新面板取消异步回发,需要一种不同的技术:

Sure enough, cancelling asynchronous postbacks from an Update Panel requires a different technique:

http://msdn.microsoft.com/en-us/magazine/ cc163413.aspx

<一个href=\"http://stackoverflow.com/questions/2424327/$p$pvent-asp-net-dopostback-from-jquery-submit-within-updatepanel\">http://stackoverflow.com/questions/2424327/$p$pvent-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;
}

function unloadMessage() {

    return 'You have unsaved changes.';
}

这code去与我在其他提到的问题:<一href=\"http://stackoverflow.com/questions/3290758/dropdownlist-not-firing-onbeforeunload-when-autopostbacktrue\">http://stackoverflow.com/questions/3290758/dropdownlist-not-firing-onbeforeunload-when-autopostbacktrue

This code goes with the problem described in my other question: http://stackoverflow.com/questions/3290758/dropdownlist-not-firing-onbeforeunload-when-autopostbacktrue

这篇关于当使用jQuery选择DROPDOWNLIST如何prevent的AutoPostBack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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