0

我有一个像这样的 onClientClick 事件:

OnClientClick="return getErrors();"- 这当然是在一个

函数的主体是:

function getErrors() {
    var errorString = "some errors";
   return $('<div id="dialog-message" title="Required"><p>' + errorString + '</p></div>').dialog(
                        {
                            modal: true,
                            width: 700,
                            buttons:
                           {
                               Ok: function () {
                                   $(this).dialog("close");
                                   return callback_ErrorAction(0);
                               },
                               Cancel: function () {
                                   $(this).dialog("close");
                                   return callback_ErrorAction(1);
                               }
                           }
                        });
return false; //omit this and OnClientClick gets undefined response variable
    }

回调函数也定义为:

function callback_ErrorAction(bool) 
{
    if (bool == 0) {
        return true;
    }
    else if (bool == 1) {
        return false;
    }
}

问题是我需要 OnClientClick 响应基于用户响应,单击确定或取消,但当前解决方案在用户甚至有机会选择确定或取消之前返回对 onClientClick 的响应。感谢您的帮助

4

1 回答 1

1

You can't return the user's response through OnClientClick because the jQuery dialog uses callbacks.

Instead, you need to always return false from your OnClientClick method and manually fire a postback when you need to:

function getErrors() {
    var errorString = "some errors";
    $('<div id="dialog-message" title="Required"><p>' + errorString + '</p></div>').dialog(
                        {
                            modal: true,
                            width: 700,
                            buttons:
                           {
                               Ok: function () {
                                   // Are you sure you want to close the dialog here? It will disappear when the page refreshes anyway
                                   $(this).dialog("close");
                                   // Manually trigger the postback
                                   __doPostBack('<%= btnSubmit.UniqueID %>', '');
                               },
                               Cancel: function () {
                                   $(this).dialog("close");
                                   // Error, do nothing as we will cancel the postback by default
                               }
                           }
                        });
    return false;
    }

See this question for more information on manually invoking the doPostBack call.

于 2013-10-25T11:06:00.090 回答