asp.net提交按钮关闭Jquery Ajax

时间:2012-02-12 17:39:28

标签: asp.net ajax json jquery

我正在使用一个asp.net页面,我在其中使用了JQuery UI对话框。该对话框有一个提交按钮。当我点击提交按钮时,对话框关闭。我想打电话给Webmethod。如果方法返回ture,那么我想关闭它,否则我想保持打开并显示错误消息。

[被修改]

    <script>

        jQuery(function () {
            var dlg = jQuery("#dialog").dialog({
                draggable: true,
                resizable: true,
                show: 'Transfer',
                hide: 'Transfer',
                width: 320,
                autoOpen: false,
                minHeight: 10,
                minwidth: 10,
                beforeClose: function () {
                    $.ajax({
                        url: "Default.aspx/GetResult",
                        success: function (response) {
                            if (response == true) {
                                ("#dialog").close()
                            }
                            else {
                                alert('asdasdds');
                            }
                        }
                    });


                    return false; //this will stop dialog box to close
                }
            });
            dlg.parent().appendTo(jQuery("form:first"));
        });

    </script>

 <div id="Result">
        Click here for the time.</div>
    <div id="dialog" style="text-align: left; display: none;">
        <asp:Button ID="btnButton" runat="server" Text="Button" OnClick="btnButton_Click" />
    </div>

怎么做。请建议。

此致 Asif Hameed

1 个答案:

答案 0 :(得分:2)

您可以使用ajax调用webmethod,然后根据响应有条件地对其进行操作。让您的webmethod返回true/false,然后您可以在客户端检查此值。

单击“提交”按钮执行此代码,不要关闭对话框。让成功经营者决定是否关闭它。

$.ajax({
    url: "urlOfTheService.asmx/methodName",
    success: function(response){
        if(response == true){
            //Code to close the dialog
        }
        else{
            //Show the error message
        }
    }
});

ajax()参考:http://api.jquery.com/jQuery.ajax/

<强>更新

使用open事件对话框附加提交处理程序以形成并执行上述代码。

        jQuery(function () {
            var dlg = jQuery("#dialog").dialog({
                draggable: true,
                resizable: true,
                show: 'Transfer',
                hide: 'Transfer',
                width: 320,
                autoOpen: false,
                minHeight: 10,
                minwidth: 10,
                open: function(){
                     $(this).find('form')
                     .unbind('submit')
                     .submit(function(){
                          var $form = $(this);
                          $.ajax({
                             url: "urlOfTheService.asmx/methodName",
                             success: function(response){
                                if(response == true){
                                   //Submit the form
                                   $form.unbind('submit')[0].submit();
                                }
                                else{
                                   //Show the error message
                                }
                           }
                         });
                         return false;
                     });
                }
            });
            dlg.parent().appendTo(jQuery("form:first"));
        });