<p>I'm using <a href="http://jquery.com/" rel="noreferrer">jQuery</a> and <a href="http://www.ericmmartin.com/projects/simplemodal/" rel="noreferrer">SimpleModal</a> in an ASP.Net project to make some nice dialogs for a web app.  Unfortunately, any buttons in a modal dialog can no longer execute their postbacks, which is not really acceptable.</p>

<p>There is one source I've found with a <a href="http://blog.hurlman.com/post/jQuery2c-simpleModal2c-and-ASPNet-postbacks-do-not-play-well-together.aspx" rel="noreferrer">workaround</a>, but for the life of me I can't get it to work, mostly because I am not fully understanding all of the necessary steps.</p>

<p>I also have a workaround, which is to replace the postbacks, but it's ugly and probably not the most reliable.  I would really like to make the postbacks work again.  Any ideas?</p>

<p>UPDATE: I should clarify, the postbacks are not working because the Javascript used to execute the post backs has broken in some way, so nothing happens at all when the button is clicked.</p>

[{'answer_id': 30623, 'author': 'd4nt', 'author_id': 1039, 'author_profile': 'https://Stackoverflow.com/users/1039', 'pm_score': 3, 'selected': False, 'text': '<p>All standard ASP.NET postbacks work by calling a __doPostBack javascript method on the page. That function submits the form (ASP.NET only really likes one form per page) which includes some hidden input field in which all the viewstate and other goodness lives.</p>\n\n<p>On the face of it I can\'t see anything in SimpalModal that would screw up your page\'s form or any of the standard hidden inputs, unless the contents of that modal happened to come from a HTTP GET to an ASP.NET page. That would result in two ASP.NET forms being rendered into one DOM and would would almost certainly screw up the __doPostBack function.</p>\n\n<p>Have you considered using the <a href="http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ModalPopup/ModalPopup.aspx" rel="noreferrer">ASP.NET AJAX ModalPopup control</a>? </p>\n'}, {'answer_id': 30846, 'author': 'Chris Zwiryk', 'author_id': 734, 'author_profile': 'https://Stackoverflow.com/users/734', 'pm_score': 2, 'selected': False, 'text': '<p>Web browsers will not POST any disabled or hidden form elements.</p>\n\n<p>So what\'s happening is:</p>\n\n<ol>\n<li>The user clicks on a button in your dialog.</li>\n<li>The button calls SimpleModal\'s close() method, hiding the dialog and the button</li>\n<li>The client POSTs the form (without the button\'s ID)</li>\n<li>The ASP.NET framework can\'t figure out which button was clicked</li>\n<li>Your server-side code doesn\'t get executed.</li>\n</ol>\n\n<p>The solution is to do whatever you need to do on the client (closing the dialog in this case) and then call __doPostback() yourself.</p>\n\n<p>For example (where "dlg" is the client-side SimpleModal dialog reference):</p>\n\n<pre><code>btn.OnClientClick = string.Format("{0}; dlg.close();",\n                        ClientScript.GetPostBackEventReference(btn, null));\n</code></pre>\n\n<p>That should hide the dialog, submit the form, and call whatever server-side event you have for that button.</p>\n\n<p>@Dan</p>\n\n<blockquote>\n  <blockquote>\n    <p>All standard ASP.NET postbacks work by calling a __doPostBack javascript method on the page.</p>\n  </blockquote>\n</blockquote>\n\n<p>asp:Buttons do not call __doPostback() because HTML input controls already submit the form.</p>\n'}, {'answer_id': 31338, 'author': 'tghw', 'author_id': 2363, 'author_profile': 'https://Stackoverflow.com/users/2363', 'pm_score': 6, 'selected': True, 'text': '<p>Both of you were on the right track.  What I realized is that SimpleModal appends the dialog to the body, which is outside ASP.Net\'s <code>&lt;form&gt;</code>, which breaks the functionality, since it can\'t find the elements.</p>\n\n<p>To fix it, I just modified the SimpleModal source to append eveything to <code>\'form\'</code> instead of <code>\'body\'</code>.  When I create the dialog, I also use the <code>persist: true</code> option, to make sure the buttons stay through opening and closing.</p>\n\n<p>Thanks everyone for the suggestions!</p>\n\n<p><em>UPDATE:</em> Version 1.3 adds an <code>appendTo</code> option in the configuration for specifying which element the modal dialog should be appended to. <a href="http://www.ericmmartin.com/projects/simplemodal/#options" rel="noreferrer">Here are the docs</a>.</p>\n'}, {'answer_id': 236607, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I have found the following works without modifying simplemodal.js:</p>\n\n<pre><code>function modalShow(dialog) {\n\n    // if the user clicks "Save" in dialog\n    dialog.data.find(\'#ButtonSave\').click(function(ev) {\n        ev.preventDefault();\n\n        //Perfom validation                \n\n        // close the dialog\n        $.modal.close();\n\n        //Fire the click event of the hidden button to cause a postback\n        dialog.data.find(\'#ButtonSaveTask\').click();\n    });\n\n    dialog.data.find("#ButtonCancel").click(function(ev) {\n        ev.preventDefault();\n        $.modal.close();\n    });\n}          \n</code></pre>\n\n<p>So instead of using the buttons in the dialog to cause the postback you prevent their submit and then find a hidden button in the form and call its click event.        </p>\n'}, {'answer_id': 407194, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 0, 'selected': False, 'text': '<p>FWIW, I\'ve updated the <a href="http://blog.hurlman.com/post/jQuery2c-simpleModal2c-and-ASPNet-postbacks-do-not-play-well-together.aspx" rel="nofollow noreferrer" title="blog.hurlman.com">blog post you pointed to</a> with come clarification, reposted here - the reasoning &amp; other details are in the blog post:</p>\n\n<p><strong>The solution (as of my last checkin before lunch):</strong> </p>\n\n<ol>\n  <li>Override the dialog\'s onClose event, and do the following: \n    <ol>\n      <li>Call the dialog\'s default Close function </li>\n\n      <li>Set the dialog div\'s innerHTML to a single &amp;nbsp; </li>\n\n      <li>Hijack __doPostBack, pointing it to a new function, newDoPostBack </li>\n    </ol>\n  </li>\n</ol>\n\n<p>From some comments I’ve seen on the web, point 1 needs some clarification.&#160; Unfortunately, I’m no longer with the same employer, and don’t have access to the code I used, but I’ll do what I can.&#160; First off, you need to override the dialog’s onClose function by defining a new function, and pointing your dialog to it, like this:</p>\n\n<blockquote>\n  <pre><code>$(\'#myJQselector\').modal({onClose: mynewClose});</code></pre>\n</blockquote>\n\n<ul>\n  <li>Call the dialog\'s default Close function.&#160; In the function you define, you should first call the default functionality (a best practice for just about anything you override usually):</li>\n\n  <li>Set the dialog div\'s innerHTML to a single &amp;nbsp; – This is not a required step, so skip it if you don’t understand this. </li>\n\n  <li>Hijack __doPostBack, pointing it to a new function, newDoPostBack </li>\n</ul>\n\n<blockquote>\n  <pre><code>function myNewClose (dialog)</code></pre>\n  <pre><code>{</code></pre>\n  <pre><code>    dialog.close();</code></pre>\n  <pre><code> </code><code>   __doPostBack = newDoPostBack;</code></pre>\n  <pre><code></code><code>}</code></pre>\n</blockquote>\n\n<ol>\n  <li>Write the newDoPostBack function: </li>\n</ol>\n\n<blockquote>\n  <pre>function newDoPostBack(eventTarget, eventArgument)\n{</pre>\n  <pre>    var theForm = document.forms[0];</pre>\n  <pre>    if (!theForm)</pre>\n  <pre>    {</pre>\n  <pre>        theForm = document.aspnetForm;</pre>\n  <pre>    }</pre>\n  <pre>&#160;</pre>\n  <pre>    if (!theForm.onsubmit || (theForm.onsubmit() != false))</pre>\n  <pre>    {</pre>\n  <pre>        document.getElementById(&quot;__EVENTTARGET&quot;).value = eventTarget;</pre>\n  <pre>        document.getElementById(&quot;__EVENTARGUMENT&quot;).value = eventArgument;</pre>\n  <pre>        theForm.submit();</pre>\n  <pre>    }\n}\n    </pre>\n</blockquote>\n'}, {'answer_id': 1141673, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>The new Jquery.simplemodal-1.3.js has an option called appendTo. So add an option called appendTo:'form' because the default is appendTo:'body' which doesn't work in asp.net. </p>\n"}, {'answer_id': 1786639, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>Had the same problem, but <code>{appendTo:'form'}</code> caused the modal popup to be rendered completely wrong (as though I had a CSS issue). </p>\n\n<p>Turns out the template I'm building on top of has includes that put other forms on the page. Once I set <code>{appendTo:'#aspnetForm'}</code> (the default Asp.net form ID), everything worked great (including the postback).</p>\n"}, {'answer_id': 2459299, 'author': 'Steve', 'author_id': 295293, 'author_profile': 'https://Stackoverflow.com/users/295293', 'pm_score': 2, 'selected': False, 'text': '<p>got caught out by this one - many thanks to tghw and all the other contributors on the appendto form instead of body fix. (resolved by attributes on the 1.3 version)</p>\n\n<p>btw: If anyone needs to close the dialog programmatically from .net - you can use this type of syntax</p>\n\n<pre><code>private void CloseDialog()\n{\n    string script = string.Format(@"closeDialog()");\n    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);\n}\n</code></pre>\n\n<p>where the javascript of closedialog is like this....</p>\n\n<pre><code>    function closeDialog() {\n        $.modal.close();\n    }\n</code></pre>\n'}, {'answer_id': 18457269, 'author': 'secretwep', 'author_id': 1161795, 'author_profile': 'https://Stackoverflow.com/users/1161795', 'pm_score': 0, 'selected': False, 'text': '<p>In addition to tghw\'s answer, this excellent blog post helped me: <a href="http://blog.amtopm.be/2009/03/18/jquery-modal-forms-break-aspnet-postback-functionality/" rel="nofollow">jQuery: Fix your postbacks in Modal forms</a> -- specifically BtnMike\'s comment: "You also must not have CssClass=”simplemodal-close” set on your asp:button."  Taking that off the class was the not-obvious-to-me solution.</p>\n\n<p>-John</p>\n'}, {'answer_id': 22773523, 'author': 'jcmordan', 'author_id': 2624360, 'author_profile': 'https://Stackoverflow.com/users/2624360', 'pm_score': -1, 'selected': False, 'text': '<p>if you don want modify the SimpleModal source.\ntry this..</p>\n\n<p>After you call the modal() method add this:</p>\n\n<pre><code>$("#simplemodal-overlay").appendTo(\'form\');\n$("#simplemodal-container").appendTo(\'form\');\n</code></pre>\n\n<p>the SimpleModal plugin add two this to your markup. </p>\n\n<ol>\n<li>\'simplemodal-overlay\' for the background</li>\n<li>\'simplemodal-container\' containig the div that you whant as pop up modal.</li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/29174', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2363/']