jQuery UI Dialog with ASP.NET button postback

前端 未结 17 1860
鱼传尺愫
鱼传尺愫 2020-11-22 14:43

I have a jQuery UI Dialog working great on my ASP.NET page:

jQuery(function() {
    jQuery(\"#dialog\").dialog({
        draggable: true,
        resizable:          


        
相关标签:
17条回答
  • 2020-11-22 15:22

    I just added the following line after you created the dialog:

    $(".ui-dialog").prependTo("form");
    
    0 讨论(0)
  • 2020-11-22 15:23

    With ASP.NET just use UseSubmitBehavior="false" in your ASP.NET button:

    <asp:Button ID="btnButton" runat="server"  Text="Button" onclick="btnButton_Click" UseSubmitBehavior="false" />       
    

    Reference: Button.UseSubmitBehavior Property

    0 讨论(0)
  • 2020-11-22 15:27

    I didn't want to have to work around this problem for every dialog in my project, so I created a simple jQuery plugin. This plugin is merely for opening new dialogs and placing them within the ASP.NET form:

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

    So to use the plugin, you first load jQuery UI and then the plugin. Then you can do something like the following:

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

    To be clear, this plugin assumes you are ready to show the dialog when you call it.

    0 讨论(0)
  • 2020-11-22 15:29

    Move the dialog the right way, but you should do it only in the button opening the dialog. Here is some additional code in jQuery UI sample:

    $('#create-user').click(function() {
        $("#dialog").parent().appendTo($("form:first"))
        $('#dialog').dialog('open');
    })
    

    Add your asp:button inside the dialog, and it runs well.

    Note: you should change the <button> to <input type=button> to prevent postback after you click the "create user" button.

    0 讨论(0)
  • 2020-11-22 15:30

    The solution from Robert MacLean with highest number of votes is 99% correct. But the only addition someone might require, as I did, is whenever you need to open up this jQuery dialog, do not forget to append it to parent. Like below:

    var dlg = $('#questionPopup').dialog( 'open'); dlg.parent().appendTo($("form:first"));

    0 讨论(0)
提交回复
热议问题