Solved

showModalDialog returning value

Posted on 2008-10-17
5
2,530 Views
Last Modified: 2013-11-07
I am developing a web application with asp.net, vb.net and ajax extensions.

I am trying to implement a modal dialog, where the user can choose Accept or Cancel an action, returning (0-Calcel or 1-Accept) like a mesagebox function, as it can be called during codebehind process, that mean not through clicking a button.

But I have a problem, using the RegisterStartupScript works in this way:
Protected Function callPage() as Boolean
Dim resp as boolean = False
Dim pagName As String = "" 'page to call as modal
Dim size As String = ""
Dim script As String = ""
Dim txt As String = "Do you want delete records?"
pagName = "MsgBox.aspx?text=" & txt
size = "status:no;dialogWidth:300px;dialogHeight:120px;dialogHide:false;help:no;scroll:yes;resizable:no;"
script = "<script language='javascript' > window.showModalDialog('" & pagName & "', 'MessageBox', '" & size & "'); </script>"
UI.ScriptManager.RegisterStartupScript(Me, Me.GetType, "Respuesta", script, False)
End Sub

When the function is called a modal window appear (msgbox.aspx).

But need to catch the answer given for the user from the modal window, so If I modify the function:

Protected Function callPage() as Boolean
Dim resp as boolean = False
Dim pagName As String = "" 'page to call as modal
Dim size As String = ""
Dim script As String = ""
Dim txt As String = "Do you want delete records?"
pagName = "MsgBox.aspx?text=" & txt
size = "status:no;dialogWidth:300px;dialogHeight:120px;dialogHide:false;help:no;scroll:yes;resizable:no;"
script = "<script language='javascript' > var value= window.showModalDialog('" & pagName & "', 'Respuesta', '" & size & " ');  " & _
" document.getElementById(" & Chr(34) & "ctl00_ContentPlaceHolder1_Respuesta" & Chr(34) & ").value=value; " & _
" If (document.getElementById(" & Chr(34) & "ctl00_ContentPlaceHolder1_Respuesta" & Chr(34) & ").value==1 " & _
" {" & _
" <%= PostBackConfirm %>" & _
"}</script>"
UI.ScriptManager.RegisterStartupScript(Me, Me.GetType, "Respuesta", script, False)
End Sub

The idea is to catch in a postback the answer from user, buth with the fuction modified any happens, that mean the modal window is not showed.

What is wrong, can any body helpme or give an example, I don't know much about script (vb or js) code.

Thanks in advance

0
Comment
Question by:aslyas
  • 3
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
Christopher Kile earned 500 total points
ID: 22743738
This article gives an excellent example of how to return values from a modal dialog window (I believe it should also work for the modeless version, as well):

http://p2p.wrox.com/topic.asp?TOPIC_ID=27848

Once you've set up your dialog with the terminate() function as described in this example, the value is returned and can be stored in any variable you assign.  I suggest being VERY careful about assuming implicit conversions (I note the comparison with an integer value of one in your non-working code as being an example not to follow).  Also, you shouldn't be calling server functions from Javascript except to assign values to be used in the initial generation of the page.  Most commonly, if one wanted to pass the value returned by a modal dialog back to the server, one would assigned it to a server control (any HTML or ASP control with runat=server) then force the document to submit itself (e.g. document.forms[0].submit(); in Javascript).  Once your server code receives the postback, one can check the value of the server control and decide what to do from there.  Do you understand this?  Does it make sense?

0
 

Author Comment

by:aslyas
ID: 22763251
I tryied to apply the example to my code.
In parent window I am defining:
<script type ="text/javascript">    
   
    function open(pag,name,size)
    {
        var Return;
        Return = window.showModalDialog(pag,name,size);
        document.getElementById("Respuesta").value = Return.Respuesta        
    }
</script>
Where "Respuesta" is a hidden field
<asp:HiddenField ID="Respuesta" runat ="server" />
It is being called in this way:

        Dim pagName As String = ""
        Dim size As String = ""
        Dim script As String = ""
        Dim txt As String = "Desea eliminar los registros del mecanismo de daño: XYZ?"
        pagName = "MsgBox.aspx?text=" & txt
        size= "status:no;dialogWidth:300px;dialogHeight:120px;dialogHide:false;help:no;scroll:yes;resizable:no;"
               script = "<script language='javascript' > open('" & pagName & "', 'MsgBox', '" & size & "'); </script>"

        UI.ScriptManager.RegisterStartupScript(Me, Me.GetType, "Respuesta", script, False)

When that code is executed a script error appear, (expected identifier)
The code in modal window is:

       function close()
        {
            var vReturnValue = new object();
            vReturnValue.Respuesta = 0;
            window.return = vReturnValue;
            window.close;
        }
       function Ok()
        {
            var vReturnValue = new object();
            vReturnValue.Respuesta = 1;
            window.return = vReturnValue;
            window.close;
        }
And I am adding in page_load this:
        cmdAceptar.Attributes.Add("onclick", "Ok()")
        cmdCancelar.Attributes.Add("onclick", "close()")

Is something missing?

How can I catch the value?

Thanks for your help.
0
 

Author Comment

by:aslyas
ID: 22771452
Finally I got my goal.
I don't know why with object Return, it did not work I had  use of the hidden field in the result.
Code in parent page
   function open2(pag,name,size)
    {
        var Return = new Object();
        Return.Respuesta= window.showModalDialog(pag,name,size);        
        document.getElementById("Respuesta").value = Return.Respuesta;
        alert("Back... " + document.getElementById("Respuesta").value);
        if (document.getElementById("Respuesta").value==1)
        {
            alert("Enter to post...");
            <%= PostBackResp %>
        }        
    }
I am calling a postback to execute some code depending of the user seleccion in modal page.


And in the final code in modal page was:
 function Close()
        {
            var vReturnValue = new Object();
            vReturnValue.Respuesta = 0;            
            window.returnValue = vReturnValue.Respuesta;
            window.close();
        }
       
       function Ok()
        {
            var vReturnValue = new Object();
            vReturnValue.Respuesta = 1;            
            window.returnValue = vReturnValue.Respuesta;
            window.close();          
        }

Thanks a lot cpkilekofp
0
 

Author Closing Comment

by:aslyas
ID: 31507225
I give the points since gave me the direction.
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 22771622
Sorry for my delayed response; I hadn't had a chance to test your code today.  Your solution is exactly what I've used in child forms before, though, and if I hadn't been hip deep in alligators today I'd probably have remembered to suggest it to you LOL.  Glad to help :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question