带有ASP.NET按钮回发的jQuery UI对话框

Pau*_*aul 294 c# asp.net jquery postback jquery-ui

我有一个jQuery UI Dialog在我的ASP.NET页面上工作得很好:

jQuery(function() {
    jQuery("#dialog").dialog({
        draggable: true,
        resizable: true,
        show: 'Transfer',
        hide: 'Transfer',
        width: 320,
        autoOpen: false,
        minHeight: 10,
        minwidth: 10
    });
});

jQuery(document).ready(function() {
    jQuery("#button_id").click(function(e) {
        jQuery('#dialog').dialog('option', 'position', [e.pageX + 10, e.pageY + 10]);
        jQuery('#dialog').dialog('open');
    });
});
Run Code Online (Sandbox Code Playgroud)

我的div:

<div id="dialog" style="text-align: left;display: none;">
    <asp:Button ID="btnButton" runat="server" Text="Button" onclick="btnButton_Click" />
</div>
Run Code Online (Sandbox Code Playgroud)

但btnButton_Click永远不会被调用......我该如何解决?

更多信息:我添加了此代码以将div移动到表单:

jQuery("#dialog").parent().appendTo(jQuery("form:first"));
Run Code Online (Sandbox Code Playgroud)

但仍然没有成功......

Rob*_*ean 314

你接近解决方案,只是得到错误的对象.它应该是这样的:

jQuery(function() {
    var dlg = jQuery("#dialog").dialog({
                         draggable: true,
                         resizable: true,
                         show: 'Transfer',
                         hide: 'Transfer',
                         width: 320,
                         autoOpen: false,
                         minHeight: 10,
                         minwidth: 10
                     });
    dlg.parent().appendTo(jQuery("form:first"));
});
Run Code Online (Sandbox Code Playgroud)


小智 37

$('#divname').parent().appendTo($("form:first"));
Run Code Online (Sandbox Code Playgroud)

使用此代码解决了我的问题,它适用于每个浏览器,Internet Explorer 7,Firefox 3和Google Chrome.我开始喜欢jQuery ......这是一个很酷的框架.

我也测试过部分渲染,正是我正在寻找的东西.大!

<script type="text/javascript">
    function openModalDiv(divname) {
        $('#' + divname).dialog({ autoOpen: false, bgiframe: true, modal: true });
        $('#' + divname).dialog('open');
        $('#' + divname).parent().appendTo($("form:first"));
    }

    function closeModalDiv(divname) {
        $('#' + divname).dialog('close');
    }
</script>
...
...
<input id="Button1" type="button" value="Open 1" onclick="javascript:openModalDiv('Div1');" />
...
...
<div id="Div1" title="Basic dialog" >
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
       <ContentTemplate>
          postback test<br />
          <asp:Button ID="but_OK" runat="server" Text="Send request" /><br />
          <asp:TextBox ID="tb_send" runat="server"></asp:TextBox><br />
          <asp:Label ID="lbl_result" runat="server" Text="prova" BackColor="#ff0000></asp:Label>
        </ContentTemplate>
    <asp:UpdatePanel>
    <input id="Button2" type="button" value="cancel" onclick="javascript:closeModalDiv('Div1');" />
</div>
Run Code Online (Sandbox Code Playgroud)


小智 34

FWIW,形式:第一种技术对我不起作用.

但是,该博客文章中的技术做到了:

http://blog.roonga.com.au/2009/07/using-jquery-ui-dialog-with-aspnet-and.html

具体来说,将其添加到对话框声明中:

  open: function(type,data) {
    $(this).parent().appendTo("form");
  }
Run Code Online (Sandbox Code Playgroud)


Mik*_*ike 27

请注意,jQuery UI v1.10中还有一个附加设置.添加了一个appendTo设置,以解决您用于将元素重新添加到表单的ASP.NET变通方法.

尝试:

$("#dialog").dialog({
     autoOpen: false,
     height: 280,
     width: 440,
     modal: true,
     **appendTo**:"form"
});
Run Code Online (Sandbox Code Playgroud)

  • parent().appendTo("form")技术不适用于模式对话框,因为overlay div保留在表单之外并最终覆盖对话框(使其无法运行).通过将这个新的appendTo属性与jquery 1.10一起使用,overlay div被放在正确的位置,因此模式对话框可以正常工作. (3认同)

nic*_*ckb 24

的上面的答案为我做了诀窍.接受的答案的问题是它只有在页面上有一个模态时才有效.如果你有多个模态,你需要在初始化对话框时在"open"参数中内联,而不是在事后.

open: function(type,data) { $(this).parent().appendTo("form"); }
Run Code Online (Sandbox Code Playgroud)

如果你做的第一个接受的答案告诉你多个模态,你只会得到页面上的最后一个模式正确地触发回发,而不是全部.


Cha*_*ert 21

主要是因为jQuery使用DOM将对话框移动到表单标记之外.将其移回到表单标记内,它应该可以正常工作.你可以通过检查Firefox中的元素来看到这一点.


Jam*_*ers 8

我不想为我的项目中的每个对话框解决这个问题,所以我创建了一个简单的jQuery插件.此插件仅用于打开新对话框并将它们放在ASP.NET表单中:

(function($) {
    /**
     * This is a simple jQuery plugin that works with the jQuery UI
     * dialog. This plugin makes the jQuery UI dialog append to the
     * first form on the page (i.e. the asp.net form) so that
     * forms in the dialog will post back to the server.
     *
     * This plugin is merely used to open dialogs. Use the normal
     * $.fn.dialog() function to close dialogs programatically.
     */
    $.fn.aspdialog = function() {
        if (typeof $.fn.dialog !== "function")
            return;

        var dlg = {};

        if ( (arguments.length == 0)
                || (arguments[0] instanceof String) ) {
            // If we just want to open it without any options
            // we do it this way.
            dlg = this.dialog({ "autoOpen": false });
            dlg.parent().appendTo('form:first');
            dlg.dialog('open');
        }
        else {
            var options = arguments[0];
            options.autoOpen = false;
            options.bgiframe = true;

            dlg = this.dialog(options);
            dlg.parent().appendTo('form:first');
            dlg.dialog('open');
        }
    };
})(jQuery);</code></pre>
Run Code Online (Sandbox Code Playgroud)

因此,要使用该插件,首先加载jQuery UI然后加载插件.然后你可以做类似以下的事情:

$('#myDialog1').aspdialog(); // Simple
$('#myDialog2').aspdialog('open'); // The same thing
$('#myDialog3').aspdialog({title: "My Dialog", width: 320, height: 240}); // With options!
Run Code Online (Sandbox Code Playgroud)

需要说明的是,此插件假定您在调用对话框时已准备好显示对话框.


小智 7

太棒了!这解决了我的ASP问题:按钮事件没有在jQuery模式中触发.请注意,使用带有以下内容的jQuery UI模式可以触发按钮事件:

// Dialog Link
$('#dialog_link').click(function () {
    $('#dialog').dialog('open');
    $('#dialog').parent().appendTo($("form:first"))
    return false;
});
Run Code Online (Sandbox Code Playgroud)

以下行是实现此功能的关键!

$('#dialog').parent().appendTo($("form:first"))
Run Code Online (Sandbox Code Playgroud)


Jec*_*kov 7

我知道这是一个老问题,但对于任何有相同问题的人来说,有一个更新更简单的解决方案:jQuery UI 1.10.0中引入了"appendTo"选项

http://api.jqueryui.com/dialog/#option-appendTo

$("#dialog").dialog({
    appendTo: "form"
    ....
});
Run Code Online (Sandbox Code Playgroud)