Take the 2-minute tour ×

I've just determined using Firebug that when Fancybox window is created it actually takes all of my ASP.NET controls (contained in DIV tag) and puts them outside FORM tag. So I guess this is the reason why then ASP.NET button doesn't do anything - it is placed outside form.

So, do you have any suggestions how I can prevent this (or make that ASP.NET button work), other than using completely different modal dialog?

EDIT: OK, people are reporting that some of the proposed fixes are working for them on certain versions. So, be sure to read all of the answers / scroll to the bottom for how to fix this issue on different Fancybox versions.

share|improve this question
saved me a question, i was about to ask this. love so's auto related question listing. –  melaos Jul 16 '10 at 7:08
I've been trying to debug this for 6 hours. I'm glad I finally found this. –  The Muffin Man May 23 '11 at 5:44
Those who still are facing problem follow this link. –  Raghav Jul 8 '13 at 9:15

11 Answers 11

up vote 31 down vote accepted

You need to change this (somewhere around line 719 of jquery.fancybox-1.3.1.js):

    tmp         = $('<div id="fancybox-tmp"></div>'),
    loading     = $('<div id="fancybox-loading"><div></div></div>'),
    overlay     = $('<div id="fancybox-overlay"></div>'),
    wrap        = $('<div id="fancybox-wrap"></div>')


share|improve this answer
Thanks man; do you also have recommendations related to partial page update - when I press button I just want region in Fancybox to refresh. Should I use UpdatePanel? How (it works in IE, not in Firefox)? Have any pointers to examples? Thanks! –  kape123 Apr 21 '10 at 22:41
You can try an update panel, I prefer to just use jquery ajax for partial page updates. Look at the jQuery.load() method, it allows you to load a div with content, iframe, etc. You could call the load and load your data into the fancybox div on the button click. –  derek Apr 21 '10 at 22:54
+1 This worked for me using Fancybox 1.3.4. –  Kynth Dec 9 '10 at 11:52
Same as above - Fancybox 1.3.4 –  JBoom Jan 25 '12 at 17:02
-1 It is bad practice to edit 3rd party source files. For version 2, check the answer from @Myke-Black for the best way to do it. –  sparebytes Nov 20 '13 at 16:29

For anyone needing a simple answer to this problem using Fancybox version 2 theres a much easier way of doing it. All you have to do is add parent: "form:first" in the code eg

    $(document).ready(function () {
            parent: "form:first",
            fitToView: true,
            width: '300px',
            height: '100px',
            autoSize: false,
            closeClick: false,
            openEffect: 'none',
            closeEffect: 'none',
            modal: false

then this will append the fancybox elements in the dom inside the form tag, rather than inside the body tag.

share|improve this answer
Finally an answer that doesn't involve editing the FancyBox source codwe. –  sparebytes Nov 20 '13 at 16:31
Dude! I don't know why that complicated workaround got all the love. This answer worked perfectly. Thanks so much! –  Stanton Dec 20 '13 at 0:09
@Stanton Because the question was posted during the reign of Fancybox 1 and as such the options provided in this answer were nonexistant. –  adaam Jul 24 at 11:38
This just saved me hours of searching... form:first worked perfect! –  MizAkita Sep 18 at 22:49
Thanks, parent: "form:first" worked perfect for me. –  Saygın Karahan Dec 4 at 12:20

Fancybox Version 2.1.4

Change these 2 lines

Around Line 2069 :

document.all && !document.querySelector ? $('html') : $('body');


document.all && !document.querySelector ? $('html') : $('form:first');

and around Line 1960 :

this.overlay = $('<div class="fancybox-overlay"></div>').appendTo('body');


this.overlay = $('<div class="fancybox-overlay"></div>').prependTo('form');

You could also use appendTo but that's up to you. In my case I needed prependTo.

share|improve this answer
The first fix no longer seems necessary, but the second is still an issue. I've opened a pull request, so hopefully this bug will go away. –  zzzzBov Mar 20 '13 at 17:44
Thanks for this fix, this WILL WORK IN ALL VERSION –  sathishkumar Apr 23 '13 at 10:07

I've been trying to figure out this problem all week, I haven't really had any luck

I just came across this article


That explains how to get postback working with the jQuery UI Dialog box.

If it's not 100% necessary to use fancy box then this method is definitely worth a try, it's saved me a lot of hassle today.

share|improve this answer

Without altering the FancyBox source, put this after the FancyBox script (outside any load events!!!):

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/#.#/jquery.min.js"></script>
<script type="text/javascript" src="/fancybox/jquery.fancybox-#.#.#.pack.js"></script>
<script type="text/javascript">

    // Override $.fancybox.init to fix ASP.NET PostBack bug;
    var fancyboxInitOld = $.fancybox.init;
    $.fancybox.init = function () {
        $("#fancybox-tmp, #fancybox-loading, #fancybox-overlay, #fancybox-wrap").appendTo("form:first");

    // Your code ...

share|improve this answer
Verified that this works in Chrome, FireFox, and IE. Seems like a preferred solution as it doesn't involve adjusting the actual scripts themselves. The downfall is that you'll need to add this bit wherever you include the script vs. having one update and it's set application wide. –  2GDave Jun 20 '12 at 14:40

jquery.fancybox-1.3.4.js line 1040


Works (my asp:Button does postback) for me on FF and IE9, thanks for the solution

share|improve this answer

The above update didn't work for me, the fancybox was still added outside the form. I then commented out those 6 lines in jquery.fancybox-1.3.2.js and found they weren't being used at all in my code.

I did a search on 'body' in the fancybox js files and changed it to 'form' in:

jquery.fancybox-1.3.2.js (ln 484)
jquery.fancybox-1.3.2.pack.js (ln 27, ln 41)

The fancybox is now getting added to the form and the server controls are working.

share|improve this answer

Fancybox Version: 2.1.2

Line 1782 of jquery.fancybox.js

Change this:

this.el     = document.all && !document.querySelector ? $('html') : $('body');

To this:

share|improve this answer

I use Fancybox 1.3.4 to show an aspx page as a popup page in fancybox iframe. But button in the popup page does not cause postback. I did not change anything as you said before. Rather I did the following.

 function check() 
            var validated = Page_ClientValidate();
            if (validated) {
                return true;
                return false;

 <asp:Button ID="bur" runat="server" Text="ser"  
            OnClientClick="check()"   />

protected void bur_Click(object sender,Eventargs e)
{ //put breakpoint here.

share|improve this answer

Fancybox 1.3.4 actually does postback.

But if is closed in OnClientClick that is before server side click event it wont postback .So The above is the solution to this problem.(Adding doPostBack in javascript function).

Normally Fancybox 1.3.4 does postback without any change in its .Js files. But to close it , need to write this line ScriptManager.RegisterClientScriptBlock(this, GetType(), "", "parent.$.fancybox.close();", true); in server side click event. Only then both serverside event gets called and fancybox also closes.

But Fancybox 2.1.3 (latest) does postback event after it is closed in OnClientClick.

share|improve this answer

If refreshing the parent is a solution which serves the purpose, you can refresh onClosed:

  'onClosed': function() {
share|improve this answer

Your Answer


