asp.net提交按钮关闭Jquery Ajax [英] asp.net submit button close Jquery Ajax

查看:62
本文介绍了asp.net提交按钮关闭Jquery Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个asp.net页面上使用JQuery UI对话.此对话框有一个提交按钮.当我单击提交"按钮时,对话框关闭.我想在上面调用一个Webmethod.如果方法返回ture,则我想关闭它,否则我想使其保持打开状态并显示错误消息.

I am working on an asp.net page where I have used JQuery UI dialogue. This dialogue has a submit button. When I click the submit button dialogue closes. I want to call a Webmethod on it. If method returns ture, then I want to close it otherwise I want to keep it open with error message shown.

[已编辑]

    <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)

Regards, Asif Hameed

推荐答案

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

You can call webmethod using ajax and then act on it conditionally based on the response. Let your webmethod just return true/false and then you can check this value on the client side.

单击提交按钮时执行此代码,并且不要关闭对话框.让成功处理程序决定是否关闭它.

Execute this code on submit button click and do not close the dialog. Let the success handler decide whether to close it or no.

$.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事件将提交处理程序附加到表单并执行上述代码.

Use open event of dialog box to attach the submit handler to form and execute the above code.

        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"));
        });

这篇关于asp.net提交按钮关闭Jquery Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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