1

For some reason, the value returned by the Modal Dialog box is always "undefined".

My Main page (aspx):

<%@ Page Title="Home Page" Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<html>
<head></head>
<body>

<script type="text/javascript">

function openWindows () {
    var getval;
    getval = window.showModalDialog('../WebSite/popups/uploader.htm');
    document.getElementById("Input").value = getval;
   }
</script>

<input id="Input" runat="server" />
<input type="button" id="Button1" runat="server" onclick="openWindows()" value="Choose Image"/>
</form>
</body>
</html>

So in this case, the value of getval is always "undefined"

My Dialog Box (HTML) Code:

<html>
<head>
    <script type="text/javascript">
        function ReturnValues() {
            var objfile = document.getElementById("fileup").value
            document.getElementById("TxtInput").value = objfile
            var vReturnValue = document.getElementById("TxtInput").value;
            window.ReturnValue = vReturnValue;
            window.close();
        }

    </script>
</head>
<body>
    <form id="Formuploader" method="post" runat="server">
        <input id="TxtInput" type="hidden" runat="server" /><br />
        <button id="btnSaveImage" runat="server" onclick="ReturnValues()">Save Image</button>
    </form>
    </body>
    </html>

Here, ReturnValue does have the required value. But as soon as the ModalDialog closes, the getval variable in the main window becomes undefined.

Any help is greatly appreciated. Thanks!

2 Answers2

0

when you return value, do it this way:

window.returnValue = vReturnValue;

use lowercase returnValue, not ReturnValue.

also, your modal dialog is not closing. to fix that, change your button to a link.

<a href="#" id="btnSaveImage" runat="server" target="_self" onclick="ReturnValue()">
        Save Image</a>
Ray Cheng
  • 12,230
  • 14
  • 74
  • 137
  • I don't get it. How is objfile undefined? Also, the value is getting populated till the the window.ReturnValue = vReturnValue; – Harsimranjit Singh May 19 '12 at 04:21
  • i updated my post. also, take a look at the following article on how to return complex data. http://p2p.wrox.com/javascript/26755-return-value-showmodaldialog.html – Ray Cheng May 19 '12 at 04:42
  • I have another question: [link]http://stackoverflow.com/questions/10662592/calling-code-behind-from-javascript – Harsimranjit Singh May 19 '12 at 05:33
  • Ray, please help: http://stackoverflow.com/questions/11367732/gridview-inside-updatepanel-on-modaldialog-not-updating-after-asyncfileupload – Harsimranjit Singh Jul 06 '12 at 18:30
0

Try this:

window.opener.document.getElementById("Input").value = getval;
Rahul
  • 76,197
  • 13
  • 71
  • 125