Modal Dialog Box - return value not always returned
<p>Could you please help me?
I've created a Modal Dialog Box based on the code shown in this article:
<a href="http://www.eggheadcafe.com/articles/javascript%5Fmodal%5Fdialog.asp" rel="nofollow">http://www.eggheadcafe.com/articles/javascript%5Fmodal%5Fdialog.asp</a></p>

<p>In my sample code, I use this dialog three times: for a hyperlink, a button Button1 with the onclick attribute added and a button Button2 with the OnClientClick event.
If I click the hyperlink, the return value from the dialog box that defines the dialog's button being clicked goes to a text box.
But if I click either Button1 or Button2, I cannot get the return value, i.e. determine which of the dialog's buttons gets clicked.</p>

<p>Could you please help me find a correct way to get the dialog's return value? 
I'm especially interested in the case of Button1 with the onclick attribute added.</p>

<p>Below is my Page and code-behind test code.</p>

<p>== Page ==</p>

<pre><code>&lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="ModalDialogTest1.aspx.vb" Inherits="ModalDialogTest1" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;script language="javascript"&gt;

var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;

ModalDialog.value = '';
ModalDialog.eventhandler = '';


function ModalDialogMaintainFocus()
{
  try
  {
    if (ModalDialogWindow.closed)
     {
        window.clearInterval(ModalDialogInterval);
        eval(ModalDialog.eventhandler);       
        return;
     }
    ModalDialogWindow.focus(); 
  }
  catch (everything) {   }
}

 function ModalDialogRemoveWatch()
 {
    ModalDialog.value = '';
    ModalDialog.eventhandler = '';
 }

 function ModalDialogShow(Title,BodyText,Buttons,EventHandler)
 {

   ModalDialogRemoveWatch();
   ModalDialog.eventhandler = EventHandler;

   var args='width=350,height=125,left=325,top=300,toolbar=0,';
       args+='location=0,status=0,menubar=0,scrollbars=1,resizable=0';  

   ModalDialogWindow=window.open("","",args); 
   ModalDialogWindow.document.open(); 
   ModalDialogWindow.document.write('&lt;html&gt;');
   ModalDialogWindow.document.write('&lt;head&gt;'); 
   ModalDialogWindow.document.write('&lt;title&gt;' + Title + '&lt;/title&gt;');
   ModalDialogWindow.document.write('&lt;script' + ' language=JavaScript&gt;');
   ModalDialogWindow.document.write('function CloseForm(Response) ');
   ModalDialogWindow.document.write('{ ');
   ModalDialogWindow.document.write(' window.opener.ModalDialog.value = Response; ');
   ModalDialogWindow.document.write(' window.close(); ');
   ModalDialogWindow.document.write('} ');
   ModalDialogWindow.document.write('&lt;/script' + '&gt;');        
   ModalDialogWindow.document.write('&lt;/head&gt;');   
   ModalDialogWindow.document.write('&lt;body onblur="window.focus();"&gt;');
   ModalDialogWindow.document.write('&lt;table border=0 width="95%" align=center cellspacing=0 cellpadding=2&gt;');
   ModalDialogWindow.document.write('&lt;tr&gt;&lt;td align=left&gt;' + BodyText + '&lt;/td&gt;&lt;/tr&gt;');
   ModalDialogWindow.document.write('&lt;tr&gt;&lt;td align=left&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;');
   ModalDialogWindow.document.write('&lt;tr&gt;&lt;td align=center&gt;' + Buttons + '&lt;/td&gt;&lt;/tr&gt;');
   ModalDialogWindow.document.write('&lt;/body&gt;');
   ModalDialogWindow.document.write('&lt;/html&gt;'); 
   ModalDialogWindow.document.close(); 
   ModalDialogWindow.focus(); 
   ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()",5);

 }

&lt;/script&gt;

&lt;script language=JavaScript&gt;
    function OKCancel_1(BodyText, EventHandler) {
     var Buttons = '';
     Buttons = '&lt;input type="submit" value="Cancel" class="butt" style="width:100px;" onclick=javascript:CloseForm("Cancel");&gt;&amp;nbsp;&amp;nbsp;';
     Buttons += '&lt;input type="submit" value="OK" class="butt" style="width:100px;" onclick=javascript:CloseForm("OK");&gt;&amp;nbsp;&amp;nbsp;';
     ModalDialogShow("Dialog", BodyText, Buttons, EventHandler);
    }

    function NoYes(BodyText, EventHandler) {
     var Buttons = '';
     Buttons = '&lt;input type="submit" value="No" class="butt" style="width:100px;" onclick=javascript:CloseForm("No");&gt;&amp;nbsp;&amp;nbsp;';
     Buttons += '&lt;input type="submit" value="Yes" class="butt" style="width:100px;" onclick=javascript:CloseForm("Yes");&gt;&amp;nbsp;&amp;nbsp;';
     ModalDialogShow("Dialog", BodyText, Buttons, EventHandler);
    }

  function OKCancelReturnMethod() {
    document.getElementById('OKCancelReturn').value = ModalDialog.value;
    ModalDialogRemoveWatch();
  }

  function NoYesReturnMethod() {
    document.getElementById('modalreturn').value = ModalDialog.value;
    ModalDialogRemoveWatch();
  }

&lt;/script&gt;

&lt;body&gt;


&lt;form id="form2" runat="server"&gt;
    &lt;div&gt;
     &lt;asp:HyperLink ID="HyperLink3" runat="server"
      NavigateUrl="javascript:OKCancel_1('OKCancel test','OKCancelReturnMethod()');"&gt;OK/Cancel_1
     &lt;/asp:HyperLink&gt;
     &lt;br /&gt;&lt;br /&gt;
     &lt;asp:Label ID="Label1" runat="server" Text="OKCancelReturn:"&gt;&lt;/asp:Label&gt;
     &lt;asp:TextBox ID="OKCancelReturn" runat="server"&gt;&lt;/asp:TextBox&gt;
     &lt;br /&gt;&lt;br /&gt;
     &lt;asp:Button ID="Button1" runat="server" Text="Button -&gt; NoYes onclick" &gt;
     &lt;/asp:Button&gt;
     &lt;br /&gt;&lt;br /&gt;
     &lt;asp:Button ID="Button2" runat="server" Text="Button -&gt; NoYes OnClientClick" 
      OnClientClick="javascript:NoYes('NoYes test','NoYesReturnMethod()');"&gt;
     &lt;/asp:Button&gt;
     &lt;br /&gt;&lt;br /&gt;
     &lt;asp:Label ID="Label2" runat="server" Text="modalreturn:"&gt;&lt;/asp:Label&gt;
     &lt;asp:TextBox ID="modalreturn" runat="server"&gt;&lt;/asp:TextBox&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>== code-behind===</p>

<pre><code>Partial Class ModalDialogTest1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim message As String
        message = "Test Message: Do you want to delete?"
        Button1.Attributes("onclick") = GetConfirmationScript(message)
     End Sub

    Private Function GetConfirmationScript(ByVal message As String) As String
        Dim output As String
        output = "javascript:NoYes('" &amp; message &amp; "','NoYesReturnMethod()');"
        Return output
    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        OKCancelReturn.Text = modalreturn.Text

    End Sub
End Class
</code></pre>

<p>Many thanks,
Lev</p>
