Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

Thanks

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

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

But still without success...

share|improve this question
LOL... at first I thought you stole my question, but then I saw you asked first. I've asked same question for FancyBox - stackoverflow.com/questions/2686362/… – kape123 Dec 2 '11 at 21:00

18 Answers

up vote 229 down vote accepted

You are close to the solution, just getting the wrong object. 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"));
});
share|improve this answer
3  
thank you, I love SO – andy Jun 30 '09 at 7:35
8  
What do I do in a usercontrol? it doesn't work. – Shimmy Jul 21 '09 at 12:52
5  
Just an fyi: the appendTo(...) function takes a jQuery selector, so you can do .appendTo("form:first") – Aren Jul 5 '10 at 18:32
4  
Just saved my life :P – psousa Mar 7 '11 at 1:28
2  
I'd like to add that if you use open function it'll not work. So, you need to add this: ,open: $(this).parent().appendTo(jQuery("form:first")); – Homam Apr 12 '11 at 7:19
show 10 more comments
$('#divname').parent().appendTo($("form:first"));

For me using this code solved my problem and work in every browser. IE7, FF3 , 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>
share|improve this answer
This full example helped me piece together what I was missing when encountering this problem. Thanks! – Dillie-O Apr 13 '11 at 23:24

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");
  }
share|improve this answer
1  
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. – Merritt Jun 25 '10 at 19:39

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.

share|improve this answer
1  
Ahh, perfect! You're a genius :) – Artiom Chilaru Jun 17 '10 at 16:00
1  
+1 Absolutely true. Fixed my problem for me. But why is it necessary? I can't figure that out. – Colin Sep 9 '11 at 16:07
Ahh, perfect! You're a genius :) – Sarawut Positwinyu Jun 20 '12 at 3:55

Primarily its 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.

share|improve this answer
I moved it inside form : jQuery("#dialog").parent().appendTo(jQuery("form:first")); But the problem still there... – Paul Apr 16 '09 at 17:57
You aren't registering any other jquery events on the btnButton object are you? – Chad Ruppert Apr 16 '09 at 18:03
No... Just btnButton_Click... – Paul Apr 16 '09 at 18:05
and when are you putting it back in the form? immediately after open? – Chad Ruppert Apr 16 '09 at 18:08
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. – Chad Ruppert Apr 16 '09 at 18:11
show 6 more comments

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

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'); // 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.

share|improve this answer

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 line...

$('#dialog').parent().appendTo($("form:first"))

is the key to get this working!

share|improve this answer

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

share|improve this answer

just add this line

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

after you created the dialog

Hope this will help.

share|improve this answer

Kind of a guess, but wouldnt binding the click event in your jQuery overwrite the regular click event that asp.net generates (aka the __doPostback...). Wouldn't you want a client side trigger button to open the modal that is different from the server control button ?

share|improve this answer

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

share|improve this answer

Just a comment, I had the same problem and since I was using a slide animation, there was an additional div added and I had compensate for that:

dlg.parent().parent().appendTo($("form:first"));

however, that caused some issue where I was unable to reopen my dialog once it was closed. Consequently I had to remove the animation all together.

Now, on the other hand, I have an issue where I can open my dialog twice, but I can't close it the second time...

share|improve this answer

Move the dialog this the right way but you should do it only in the button opening the dialog. Here is some additional codes 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 to to prevent postback after you click the "create user" button.

share|improve this answer

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 avaliable to insert in your database. Don't forget to change the dialog variable to match yours.

share|improve this answer

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"); 
  },
share|improve this answer

The exact solution is;

$("#dialogDiv").dialog({ other options...,
    open: function (type, data) {
        $(this).parent().appendTo("form");
    }
});
share|improve this answer

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

share|improve this answer
2  
I believe you can just set UseSubmitBehavior property of Button to false value. This way you don't need any append or prepend calls. – Yuriy Rozhovetskiy Apr 17 at 10:27

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" />       

http://msdn.microsoft.com/pt-br/library/system.web.ui.webcontrols.button.usesubmitbehavior(v=vs.100).aspx

share|improve this answer

protected by Community May 8 at 11:13

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.