Solved

showModalDialog returning value

Posted on 2008-10-17
5
2,515 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now