【问题标题】:asp.net submit button close Jquery Ajaxasp.net 提交按钮关闭 Jquery Ajax
【发布时间】:2012-02-12 17:39:27
【问题描述】:

我正在使用 JQuery UI 对话的 asp.net 页面。此对话框有一个提交按钮。当我单击提交按钮时,对话框关闭。我想在它上面调用一个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>

怎么做。请提出建议。

问候, 阿西夫·哈米德

【问题讨论】:

    标签: asp.net ajax json jquery


    【解决方案1】:

    您可以使用ajax 调用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"));
            });
    

    【讨论】:

    • 感谢您的快速回复。如何将此代码与 jquery UI 的对话框一起使用?
    • 当您单击提交或关闭按钮关闭对话框时,您必须基本上执行此代码。关闭对话框的代码在哪里?
    • 它将有一个登录表单(一个表单、两个文本框和提交按钮以及一个forgotpass链接......当用户单击forgotpass时,登录表单将关闭并且frgotpassform将打开而不关闭对话)
    • 显示当您提交此表单时对话框会关闭,因为整个页面已刷新,对吗?或者你必须要一个代码来显式关闭对话框?
    • 它应该会自动关闭。我使用了上面的代码,但它不起作用。我粘贴了有问题的代码。
    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多