繁体   English   中英

如何使用jQuery创建ASP.NET回发确认对话框?

[英]How do I make an ASP.NET postback-confirmation dialog with jQuery?

我正在使用下面的代码来获取Jquery UI弹出窗口,但是它没有返回值并且弹出框正在关闭

我需要在单击按钮时将其用作确认框..!

代码:

<style type="text/css" >
        .ui-widget-header, .ui-widget-content
    { color: red !important;}
</style>
<script type="text/javascript">
    $(document).ready(function () {
        var $myDialog = $('<div></div>')
        .html('You are going to Lock the Record.<br/>Click OK to confirm.  Click Cancel to stop this action.')
        .dialog({
            autoOpen: false,
            title: 'Confirm..?',
            buttons: { "OK": function () {
                $(this).dialog("close");

            }, "Cancel": function () {
                $(this).dialog("close");

            }
            }
        });

        $(function fn() {
            $('#Button1').click(function (e) {
                return $myDialog.dialog('open');

            });
        });
    });
</script>


<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return fn() onclick="Button1_Click"/>
$(function fn() {

 });

你在另一个里面有一个dom ready函数。u需要删除上面的代码块...

 $(document).ready(function () {
        var $myDialog = $('<div></div>')
        .html('You are going to Lock the Record.<br/>Click OK to confirm.  Click Cancel to stop this action.')
        .dialog({
            autoOpen: false,
            title: 'Confirm..?',
            buttons: { "OK": function () {
                $(this).dialog("close");

            }, "Cancel": function () {
                $(this).dialog("close");

            }
            }
        });
            $('<%=Button1.ClientID%>').click(function (e) {
                $myDialog.dialog('open');
                return false;

            });
    });

我希望这是对的。

.dialog('open')始终会立即返回,不会等待用户操作。

您需要更改代码,以使Button1控件回发回发。 而不是依赖返回值。

有关使用JavaScript触发ASP.NET回发的信息,请参见此处。

编辑

<style type="text/css" >
        .ui-widget-header, .ui-widget-content
    { color: red !important;}
</style>
<script type="text/javascript">
    $(document).ready(function () {
        var $myDialog = $('<div></div>')
        .html('You are going to Lock the Record.<br/>Click OK to confirm.  Click Cancel to stop this action.')
        .dialog({
            autoOpen: false,
            title: 'Confirm..?',
            buttons: { "OK": function () {
                $(this).dialog("close");

__doPostBack("<%=Button1.ClientID %>","");

            }, "Cancel": function () {
                $(this).dialog("close");

            }
            }
        });

        $(function fn() {
            $('#Button1').click(function (e) {
                return $myDialog.dialog('open');

            });
        });
    });
</script>


<asp:Button ID="Button1" runat="server" UseSubmitBehavior="false" Text="Button" OnClientClick="return fn()" onclick="Button1_Click"/>

很难理解您的dom的结构。

但我会给你一个可行的解决方案

<div id="someDialog">some controls or text or whatever you want to show./div>// add it to your page.

然后使用Jquery和Jquery UI库,将其放入head标签

$(function(){
$('#someDialog').dialog({ autoOpen: false,
            title: 'Confirm..?',
            buttons: { "OK": function () {
                if(confirm("are you sure you want to close the dialog))
                     $(this).dialog("close");

            }, "Cancel": function () {
                $(this).dialog("close");

            });


 $('#Button1').click(function (e) {
                $('#someDialog').dialog('open');

            }); // without return statement. 
});

在HTML输出中是否以ID'Button1'呈现了asp.net按钮? 如果是,则可以尝试遵循以下代码(已修改代码)

代码:

<style type="text/css" >
        .ui-widget-header, .ui-widget-content
    { color: red !important;}
</style>
<script type="text/javascript">
    $(document).ready(function () {
        var $myDialog = $('<div></div>')
        .html('You are going to Lock the Record.<br/>Click OK to confirm.  Click Cancel to stop this action.')
        .dialog({
            autoOpen: false,
            title: 'Confirm..?',
            buttons: { "OK": function () {
                $(this).dialog("close");

            }, "Cancel": function () {
                $(this).dialog("close");

            }
            }
        });

         $('#Button1').click(function (e) {
               $myDialog.dialog('open');
               e.preventDefault();      
          });
      });
</script>


<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/>

添加CSS类删除以进行控制,然后应用jquery确认对话框:

$('.delete').click(function () {
          return confirm('Are you sure you wish to delete this record?'); 
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM