views:

20705

answers:

12

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

A: 

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 ?

Mcbeev
+3  A: 

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.

Chad Ruppert
I moved it inside form : jQuery("#dialog").parent().appendTo(jQuery("form:first"));But the problem still there...
Paul
You aren't registering any other jquery events on the btnButton object are you?
Chad Ruppert
No... Just btnButton_Click...
Paul
and when are you putting it back in the form? immediately after open?
Chad Ruppert
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
Yes... The div is inside form...
Paul
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?
Chad Ruppert
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...
Paul
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.
Chad Ruppert
+44  A: 

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"));
});
Robert MacLean
thank you, I love SO
andy
What do I do in a usercontrol? it doesn't work.
Shimmy
Awesome. Solved a similar problem for me.
Eli
Thanks man. Nice solution.
engineerachu
Just what I was looking for, thanks.
Abe Miessler
Just an fyi: the `appendTo(...)` function takes a jQuery selector, so you can do `.appendTo("form:first")`
Aren
sweet.. .u're legend mate!
A: 

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.

+10  A: 
**$('#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>
Marco
+3  A: 

Robert has answered it well. If you are looking complete example code check out http://blog.roonga.com.au/2009/07/using-jquery-ui-dialog-with-aspnet-and.html

ravi
A: 

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

Yahya
+5  A: 

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

ken
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
A: 

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

Mr W
+5  A: 

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.

nickb
Ahh, perfect! You're a genius :)
Artiom Chilaru
A: 

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!

Mark Houldridge