18

I made a small calendar popup in Javascript. Very simple, using the Calendar control from ASP.NET. I call the popup window with showModalDialog. In the modal window, changing the current month of the calendar causes problems because of the postback, and I found in several places that the solution is to put:

<base target="_self"/>

in the head part of the aspx file. Everything works great... except for one thing, and only in Google Chrome. To get back the selected date, I set the returnValue of the popup to the date selected in the calendar. In IE and Firefox, it always works. In Chrome, however, it works only if I don't change the current month in the calendar. As soon as I change it, the return value is not passed back to the caller of showModalDialog. It is as if the modal window is not the original one anymore; the return value is undefined.

Has anyone experienced that behavior and have a suggestion to make it work? I tried using dialogArguments to keep trace of the caller window but it gets passed only to the first modal window (it is lost after changing the current month).

The code in the calling procedure:

var d = window.showModalDialog(...)

The code in the modal window:

window.returnValue = selectedDate; 
self.close();

As I said to Teemu, selectedDate and window.returnValue are both always correct. However, in the case of Google Chrome (after a month change in the calendar), returnValue is not passed back by showModalDialog and d is undefined.

gws
  • 459
  • 1
  • 7
  • 16
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146
  • Sounds more like your procedure to change the `returnValue` in the modal dialog fails in Chrome. – Teemu Apr 18 '12 at 16:24
  • In that case, why does it work when I stay in the current month? – ConnorsFan Apr 18 '12 at 16:26
  • Hard to say without seeing the code... – Teemu Apr 18 '12 at 16:27
  • How have you defined the `selectedDate`? Try to consoleLog or alert it before closing the window, you'll find out if the value is OK. – Teemu Apr 18 '12 at 16:41
  • selectedDate is always valid, and returnValue is always correct too. But, in the case that I explain in the post (Chrome + month change), returnValue is not returned by showModalDialog. – ConnorsFan Apr 18 '12 at 17:00

2 Answers2

24

In order to keep using showModalDialog in my page, I had to come up with my own workaround for the bug. So, here it is...

In Google Chrome, after a postback, showModalDialog always returns undefined. However, the window.opener property in the modal dialog points to the caller window, even after postbacks. So, I thought about putting the result of the dialog in the returnValue property of that caller window. And it works.

In the caller window:

var prevReturnValue = window.returnValue; // Save the current returnValue
window.returnValue = undefined;
var dlgReturnValue = window.showModalDialog(...);
if (dlgReturnValue == undefined) // We don't know here if undefined is the real result...
{
    // So we take no chance, in case this is the Google Chrome bug
    dlgReturnValue = window.returnValue;
}
window.returnValue = prevReturnValue; // Restore the original returnValue

At this point, use dlgReturnValue for further processing

In the modal dialog window:

if (window.opener)
{
    window.opener.returnValue = dateValue;
}
window.returnValue = dateValue;
self.close();
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146
  • 5
    +1 for a hard work. You really have walked around the bug. I tried to reproduce your problem last night, but I couldn't get the `returnValue` even from unchanged dialog... – Teemu Apr 19 '12 at 20:52
  • 1
    I think [this is the original Chromium defect](http://code.google.com/p/chromium/issues/detail?id=42939) – robertc May 22 '12 at 15:50
  • FYI it's almost two years later and Chrome still requires this workaround. – SouthShoreAK Jan 15 '14 at 23:38
  • FYI and now it's market "won't fix" because showModalDialog will be removed completely from Chrome in future :( https://code.google.com/p/chromium/issues/detail?id=42939#c36 – DarkSide Mar 13 '14 at 15:08
  • aannnnddd.. it's gone. http://blog.chromium.org/2014/07/disabling-showmodaldialog.html – lambinator Sep 05 '14 at 16:51
  • Yep. I had to find another workaround to make modeless windows react like modal dialog boxes. – ConnorsFan Sep 08 '14 at 14:20
  • @ConnorsFan I've created such a question, mind to answer it : http://stackoverflow.com/q/26907100/1299675? – Sam Su Nov 13 '14 at 10:51
0

I had this same error, what i found in some forum is that if you put your controls in a updatePanel and ContentTemplate it will work:

 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
           <ContentTemplate>
           </ContentTemplate>
 </asp:UpdatePanel>
Bachask8
  • 310
  • 1
  • 7
  • 18