I am trying to do the following: On a page have a button that fires a code-behind function (to perform some server-side actions) which in turn calls a javascript function to launch a dialog window. The dialog window accepts some user input and upon the user's pushing a button, fires a code-behind function (to perform some server-side actions) then return a value to the calling page.
What follows is a stripped-down test to try to accomplish this. Everything works, except...
The value is never displayed in the TextBox (txtRetval) on MainForm (see the two line in jsPopupClass.vb marked ****). I have confirmed, using 'alert', that the text typed into DialogForm is indeed being returned.
Would you please help me fix this code. Thanks!
=== MainForm.aspx ===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MainForm.aspx.vb" Inherits="testModal.MainForm"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="True" name="vs_snapToGrid">
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:button id="cmdOpen" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px" runat="server"
Width="184px" Height="32px" Text="Open dialog"></asp:button>
<asp:TextBox id="txtRetval" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server"
Width="184px">Return test will go here</asp:TextBox></form>
</body>
</HTML>
=== MainForm.aspx.vb ====
Public Class MainForm
Inherits System.Web.UI.Page
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents cmdOpen As System.Web.UI.WebControls.Button
Protected WithEvents txtRetval As System.Web.UI.WebControls.TextBox
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub
Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
' do some server-side stuff, like database query, etc
Dim addTestPopup As New jsPopupClass
addTestPopup.doPopup(Me)
End Sub
End Class
=== DialogForm.aspx ===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DialogForm.aspx.vb" Inherits="testModal.DialogForm"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>DialogForm</title>
<meta name="vs_snapToGrid" content="True">
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" </HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtEntry" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 48px" runat="server"
Height="32px" Width="184px"></asp:TextBox>
<asp:Button id="cmdClose" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 88px" runat="server"
Height="32px" Width="184px" Text="Close dialog"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server"
Height="24px" Width="176px">Text to return</asp:Label>
</form>
</body>
</HTML>
=== DialogForm.aspx.vb ===
Public Class DialogForm
Inherits System.Web.UI.Page
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents cmdClose As System.Web.UI.WebControls.Button
Protected WithEvents txtEntry As System.Web.UI.WebControls.TextBox
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
' do some server-side stuff, like database insert, etc
Dim Script As String = vbCrLf + "<script language=JavaScript id='CloseDialog'>" + vbCrLf
Script += "window.returnValue = '" + txtEntry.Text + "'; " + vbCrLf
Script += "window.close();" + vbCrLf
Script += "</script>" + vbCrLf
If (Not Page.IsStartupScriptRegistered("CloseDialog") Then
Page.RegisterStartupScript("CloseDialog", Script)
End If
End Sub
End Class
=== jsPopupClass.vb ===
Public Class jsPopupClass
Public Sub doPopup(ByVal myPage As System.Web.UI.Page)
Dim Script As String = vbCrLf + "<script language=JavaScript id='PopupPage'>" + vbCrLf
Script += "var retText = null;" + vbCrLf
Script += "var WinSettings = 'center:yes;resizable:no;dialogHeight:200px';" + vbCrLf
Script += "retText = window.showModalDialog('DialogForm.aspx', null, WinSettings);" + vbCrLf
Script += "var objForm = document.forms['Form1']; " + vbCrLf ****
Script += "var objFormField = objForm.elements['txtRetval']; " + vbCrLf ****
Script += "objFormField.Text = retText;" + vbCrLf
Script += "</script>" + vbCrLf
If (Not myPage.IsStartupScriptRegistered("PopupPage") Then
myPage.RegisterStartupScript("PopupPage", Script)
End If
End Sub
End Class
----
Gerry Roston
gerry@pairofdocs.net
What follows is a stripped-down test to try to accomplish this. Everything works, except...
The value is never displayed in the TextBox (txtRetval) on MainForm (see the two line in jsPopupClass.vb marked ****). I have confirmed, using 'alert', that the text typed into DialogForm is indeed being returned.
Would you please help me fix this code. Thanks!
=== MainForm.aspx ===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MainForm.aspx.vb" Inherits="testModal.MainForm"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="True" name="vs_snapToGrid">
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:button id="cmdOpen" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px" runat="server"
Width="184px" Height="32px" Text="Open dialog"></asp:button>
<asp:TextBox id="txtRetval" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server"
Width="184px">Return test will go here</asp:TextBox></form>
</body>
</HTML>
=== MainForm.aspx.vb ====
Public Class MainForm
Inherits System.Web.UI.Page
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents cmdOpen As System.Web.UI.WebControls.Button
Protected WithEvents txtRetval As System.Web.UI.WebControls.TextBox
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub
Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOpen.Click
' do some server-side stuff, like database query, etc
Dim addTestPopup As New jsPopupClass
addTestPopup.doPopup(Me)
End Sub
End Class
=== DialogForm.aspx ===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="DialogForm.aspx.vb" Inherits="testModal.DialogForm"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>DialogForm</title>
<meta name="vs_snapToGrid" content="True">
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" </HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtEntry" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 48px" runat="server"
Height="32px" Width="184px"></asp:TextBox>
<asp:Button id="cmdClose" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 88px" runat="server"
Height="32px" Width="184px" Text="Close dialog"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 16px; POSITION: absolute; TOP: 24px" runat="server"
Height="24px" Width="176px">Text to return</asp:Label>
</form>
</body>
</HTML>
=== DialogForm.aspx.vb ===
Public Class DialogForm
Inherits System.Web.UI.Page
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents cmdClose As System.Web.UI.WebControls.Button
Protected WithEvents txtEntry As System.Web.UI.WebControls.TextBox
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
' do some server-side stuff, like database insert, etc
Dim Script As String = vbCrLf + "<script language=JavaScript id='CloseDialog'>" + vbCrLf
Script += "window.returnValue = '" + txtEntry.Text + "'; " + vbCrLf
Script += "window.close();" + vbCrLf
Script += "</script>" + vbCrLf
If (Not Page.IsStartupScriptRegistered("CloseDialog") Then
Page.RegisterStartupScript("CloseDialog", Script)
End If
End Sub
End Class
=== jsPopupClass.vb ===
Public Class jsPopupClass
Public Sub doPopup(ByVal myPage As System.Web.UI.Page)
Dim Script As String = vbCrLf + "<script language=JavaScript id='PopupPage'>" + vbCrLf
Script += "var retText = null;" + vbCrLf
Script += "var WinSettings = 'center:yes;resizable:no;dialogHeight:200px';" + vbCrLf
Script += "retText = window.showModalDialog('DialogForm.aspx', null, WinSettings);" + vbCrLf
Script += "var objForm = document.forms['Form1']; " + vbCrLf ****
Script += "var objFormField = objForm.elements['txtRetval']; " + vbCrLf ****
Script += "objFormField.Text = retText;" + vbCrLf
Script += "</script>" + vbCrLf
If (Not myPage.IsStartupScriptRegistered("PopupPage") Then
myPage.RegisterStartupScript("PopupPage", Script)
End If
End Sub
End Class
----
Gerry Roston
gerry@pairofdocs.net