jQuery UI Dialog with ASP.NET button postback
Asked Answered
P

17

299

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

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

My div:

<div id="dialog" style="text-align: left;display: none;">
    <asp:Button ID="btnButton" runat="server" Text="Button" onclick="btnButton_Click" />
</div>

But the btnButton_Click is never called... How can I solve that?

More information: I added this code to move div to form:

jQuery("#dialog").parent().appendTo(jQuery("form:first"));

But still without success...

Phenica answered 16/4, 2009 at 17:36 Comment(2)
LOL... at first I thought you stole my question, but then I saw you asked first. I've asked same question for FancyBox - #2686862Cypsela
There is a better answer than explicitly calling appendTo(). Check this https://mcmap.net/q/101869/-input-inside-jquery-ui-dialog-not-being-sentKirkwall
S
315

You are close to the solution, just getting the wrong object. It should be like this:

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"));
});
Stagey answered 20/4, 2009 at 13:5 Comment(1)
what if it has a modal option set to true? in my case the solution is perfect, but without the modal - with the modal, the div created for the modal effect is place over the dialog with the parent() methodPokey
T
37
$('#divname').parent().appendTo($("form:first"));

Using this code solved my problem and it worked in every browser, Internet Explorer 7, Firefox 3, and Google Chrome. I start to love jQuery... It's a cool framework.

I have tested with partial render too, exactly what I was looking for. Great!

<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>
Tulley answered 13/5, 2009 at 9:6 Comment(1)
This full example helped me piece together what I was missing when encountering this problem. Thanks!Supper
V
34

FWIW, the form:first technique didn't work for me.

However, the technique in that blog article did:

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

Specifically, adding this to the dialog declaration:

  open: function(type,data) {
    $(this).parent().appendTo("form");
  }
Verism answered 11/11, 2009 at 23:16 Comment(4)
if you don't do it in a update panel, this method is the only way I could get my buttons (which I want to do a full postback) to work.Latterly
+1 for this this is the only code the worked for me. im not using update panel. THANK YOU!!! saved my day!Army
+1: This worked for me when the solutions above did not work. And I am using the UpdatePanel.Projective
The simplest and best solution to getting the Dialog box back into the Form for PostBack behavior.Infanta
F
29

Be aware that there is an additional setting in jQuery UI v1.10. There is an appendTo setting that has been added, to address the ASP.NET workaround you're using to re-add the element to the form.

Try:

$("#dialog").dialog({
     autoOpen: false,
     height: 280,
     width: 440,
     modal: true,
     **appendTo**:"form"
});
Furtek answered 11/5, 2013 at 11:1 Comment(3)
This doesn't work for me with 2 jquery dialogs on the same page. I wish it would as it's the right solution.Extensity
The parent().appendTo("form") techniques did not work with a modal dialog because the overlay div stays outside the form and ends up covering the dialog (rendering it inoperable). By using this new appendTo property with jquery 1.10, the overlay div was put in the right place, so the modal dialog worked correctly.Bradway
This should be the correct answer now because jquery UI library has been updated to add appendTo setting. Thank you @Furtek saved me lot of time.Mishamishaan
B
25

ken's answer above did the trick for me. The problem with the accepted answer is that it only works if you have a single modal on the page. If you have multiple modals, you'll need to do it inline in the "open" param while initializing the dialog, not after the fact.

open: function(type,data) { $(this).parent().appendTo("form"); }

If you do what the first accepted answer tells you with multiple modals, you'll only get the last modal on the page working firing postbacks properly, not all of them.

Babyblueeyes answered 28/1, 2010 at 21:25 Comment(1)
+1 Absolutely true. Fixed my problem for me. But why is it necessary? I can't figure that out.Curr
C
21

Primarily it's because jQuery moves the dialog outside of the form tags using the DOM. Move it back inside the form tags and it should work fine. You can see this by inspecting the element in Firefox.

Corollary answered 16/4, 2009 at 17:49 Comment(9)
I moved it inside form : jQuery("#dialog").parent().appendTo(jQuery("form:first")); But the problem still there...Phenica
You aren't registering any other jquery events on the btnButton object are you?Corollary
and when are you putting it back in the form? immediately after open?Corollary
jQuery(function() { jQuery("#dialog").dialog({ draggable: true, resizable: true, show: 'Transfer', hide: 'Transfer', width: 320, autoOpen: false, minHeight: 10, minwidth: 10 }); jQuery("#dialog").parent().appendTo(jQuery("form:first")); }); is what it should be.Corollary
and still no joy? at the very least when you click the button, if in a form, it should cause a post. Are you getting JS errors at all? The modal is not closing or anything?Corollary
No JS error, and the dialog is always open... But I looked the Html generated, and the button is like that: <input type="submit" name="ctl00$ContentPlaceHolder1$btnButton" value="Button" id="ctl00_ContentPlaceHolder1_Button2" /> No onclick method listed... Strange...Phenica
Buttons don't get an onclick event clientside. They only post back. If your button is NOT posting the form, its still not inside the form as far as the DOM is concerned. You can verify this by dropping an asp:button on a form and not adding a handler for it. It will still post.Corollary
@Chad Ruppert: I will be disappointed if you gain or lose any reputation. (he's at 1337 currently) /geekVerine
@Kyle yeah, I had that for a while. Lost it in a post yesterday. :/Corollary
S
8

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.

Scutellation answered 10/11, 2010 at 16:18 Comment(0)
A
8

I know this is an old question, but for anyone who have the same issue there is a newer and simpler solution: an "appendTo" option has been introduced in jQuery UI 1.10.0

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

$("#dialog").dialog({
    appendTo: "form"
    ....
});
Abdu answered 14/12, 2013 at 22:56 Comment(2)
might be too early for a modal dialogJasper
Working charmlyMyers
T
7

Fantastic! This solved my problem with ASP:Button event not firing inside jQuery modal. Please note, using the jQuery UI modal with the following allows the button event to fire:

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

The following line is the key to get this working!

$('#dialog').parent().appendTo($("form:first"))
Thompkins answered 18/5, 2010 at 13:12 Comment(0)
H
3

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

$(".ui-dialog").prependTo("form");
Hadleigh answered 10/1, 2012 at 7:1 Comment(0)
N
3

This was the clearest solution for me

var dlg2 = $('#dialog2').dialog({
        position: "center",
        autoOpen: false,
        width: 600,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
    });

dlg2.parent().appendTo('form:first');
$('#dialog_link2').click(function(){
    dlg2.dialog('open');

All the content inside the dlg2 will be available to insert in your database. Don't forget to change the dialog variable to match yours.

Nudibranch answered 15/1, 2013 at 18:7 Comment(0)
C
3

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

Cohin answered 8/5, 2013 at 20:29 Comment(0)
I
1

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.

Iselaisenberg answered 23/4, 2009 at 10:55 Comment(0)
U
1

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

Usually answered 21/7, 2009 at 14:5 Comment(1)
as stated in my answer above over 2 years ago, but thanks anyway!Furtek
D
1

Use this line to make this work while using the modal:true option.

open: function (type, data) { 
    $('.ui-widget-overlay').appendTo("form"); $(this).parent().appendTo("form"); 
  },
Draft answered 5/4, 2013 at 18:0 Comment(0)
A
1

The $('#dialog').parent().appendTo($("form:first")) solution works fine in IE 9. But in IE 8 it makes the dialog appear and disappear directly. You can't see this unless you place some alerts so it seems that the dialog never appears. I spend one morning finding a solution that works on both versions and the only solution that works on both versions 8 and 9 is:

$(".ui-dialog").prependTo("form");

Hope this helps others that are struggeling with the same issue

Allowedly answered 17/4, 2013 at 10:20 Comment(1)
I believe you can just set UseSubmitBehavior property of Button to false value. This way you don't need any append or prepend calls.Wolff
H
0

The exact solution is;

$("#dialogDiv").dialog({ other options...,
    open: function (type, data) {
        $(this).parent().appendTo("form");
    }
});
Hexahedron answered 17/4, 2013 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.