ASP.NET - VB.NET - JavaScript Question

T

Thomas

I'm stuck on trying to get the following to work:

I am using VB.NET to program ASP.NET...my task is to create two pages,
Main and Child. In Main, I have linked a javascript function to a
button in order to popup a second window (Child) where a user can
enter information, hit the button, and the text is passed back to the
calling window (Main). I've successfully done this in classic HTML
before, but for some reason it doesn't seem like my text object on the
Main page is being referenced correctly. I'm not sure if there is a
step between .NET and javascript I am missing. In classic ASP, you
referenced HTML controls by name, but since that is no longer
available, I reference by clientID...is this a problem?

The code is as follows:

-----------
MAIN.ASPX
-----------
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="main.aspx.vb" Inherits="WebApplication1.main"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>main</title>
<script id="clientEventHandlersJS" language="javascript">
<!--

var dialogArguments;
function getValue (field){
dialogArguments = field;
open('child.aspx', 'popup', 'width=400,height=300,scrollbars=1')
}

//-->

</script>
</HEAD>
<body>
<form id="formMain" method="post" runat="server">
<P>&nbsp;</P>
<P>
<asp:Button id="buttonMain" runat="server" Text="Get
Input"></asp:Button></P>
<P>
<asp:TextBox id="textMain" runat="server"></asp:TextBox></P>
</form>
</body>
</HTML>

---------------
MAIN.ASPX.VB
---------------

Public Class main
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents button1 As System.Web.UI.WebControls.Button
Protected WithEvents firstName As
System.Web.UI.WebControls.TextBox
Protected WithEvents buttonMain As
System.Web.UI.WebControls.Button
Protected WithEvents textMain 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
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

buttonMain.Attributes.Add("onClick",
"getValue(document.getElementById('" & textMain.ClientID & "'))")

End Sub

End Class

------------
CHILD.ASPX
------------
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="child.aspx.vb" Inherits="WebApplication1.child"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>child</title>
<script id="clientEventHandlersJS" language="javascript">
<!--

function buttonChild_onclick(){
var doc = document.forms[0];
if (doc.textChild.value == ""){
alert("Email message is empty.");
return false;
}
else
{
//alert(doc.textChild.value);
window.opener.dialogArguments.value = this.textChild.value;
window.close()
}}

//-->
</script>
</HEAD>
<body>
<form id="formChild" method="post" runat="server">
<asp:Button id="buttonChild" style="Z-INDEX: 101; LEFT: 216px;
POSITION: absolute; TOP: 136px"
runat="server" Text="Submit"></asp:Button>
<asp:TextBox id="textChild" style="Z-INDEX: 102; LEFT: 48px;
POSITION: absolute; TOP: 136px"
runat="server"></asp:TextBox>
</form>
</body>
</HTML>


--------------
CHILD.ASPX.VB
--------------
Public Class child
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents button1 As System.Web.UI.WebControls.Button
Protected WithEvents buttonChild As
System.Web.UI.WebControls.Button
Protected WithEvents textChild 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
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

buttonChild.Attributes.Add("onClick", "return
buttonChild_onclick()")

End Sub

End Class
 
J

Jorge Ponte

Hi Thomas

Try This:

--------------
MAIN.ASPX
--------------

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="main.aspx.vb"
Inherits="PopUpSample.main"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>main</title>
<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="http://schemas.microsoft.com/intellisense/ie5">
<script id="clientEventHandlersJS" language="javascript">


function getValue (src){

open('child.aspx?src=' + src, 'popup', 'width=400,height=300,scrollbars=1')
}

</script>
</HEAD>
<body>
<form id="formMain" method="post" runat="server">
<P> </P>
<P>
<asp:Button id="buttonMain" runat="server" Text="Get
Input"></asp:Button></P>
<P>
<asp:TextBox id="textMain" runat="server"></asp:TextBox></P>
</form>
</body>
</HTML>

-------------------
MAIN.ASPX.VB
-------------------
Public Class main
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents button1 As System.Web.UI.WebControls.Button
Protected WithEvents firstName As System.Web.UI.WebControls.TextBox
Protected WithEvents buttonMain As System.Web.UI.WebControls.Button
Protected WithEvents textMain 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
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

buttonMain.Attributes.Add("onClick", "getValue('textMain')")

End Sub

End Class


---------------
CHILD.ASPX
---------------

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="child.aspx.vb"
Inherits="PopUpSample.child"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>child</title>
<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="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language="javascript" id="clientEventHandlersJS">
function buttonChild_onclick(){
var doc = document.forms[0];
if (doc.textChild.value == ""){
alert("Email message is empty.");
return false;
}
else
{
//alert(doc.textChild.value);
window.opener.dialogArguments.value = this.textChild.value;
window.close()
}
}
</script>
</HEAD>
<body>
<form id="formChild" method="post" runat="server">
<asp:button id="buttonChild" style="Z-INDEX: 101; LEFT: 216px; POSITION:
absolute; TOP: 136px"
runat="server" Text="Submit"></asp:button><asp:textbox id="textChild"
style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 136px"
runat="server"></asp:textbox></form>
</body>
</HTML>


------------
CHILD.ASPX.VB
------------

Imports System.Text

Public Class child
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents button1 As System.Web.UI.WebControls.Button
Protected WithEvents buttonChild As System.Web.UI.WebControls.Button
Protected WithEvents textChild 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
'CODEGEN: This method call is required by the Web Form Designer()
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'buttonChild.Attributes.Add("onClick", "return buttonChild_onclick()")
End Sub

Private Sub buttonChild_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles buttonChild.Click

Dim sbScript As New StringBuilder
sbScript.Append("<script language='javascript'>")
sbScript.Append(Environment.NewLine)
sbScript.Append("window.opener.document.forms[0].")
sbScript.Append(Request.QueryString("src"))
sbScript.Append(".value = '")
sbScript.Append(textChild.Text)
sbScript.Append("';")
sbScript.Append(Environment.NewLine)
sbScript.Append("window.close();")
sbScript.Append(Environment.NewLine)
sbScript.Append("</script>")
RegisterStartupScript("CloseWindow", sbScript.ToString)

End Sub
End Class

-------------------------

Hope it works.

Jorge Ponte











Thomas said:
I'm stuck on trying to get the following to work:

I am using VB.NET to program ASP.NET...my task is to create two pages,
Main and Child. In Main, I have linked a javascript function to a
button in order to popup a second window (Child) where a user can
enter information, hit the button, and the text is passed back to the
calling window (Main). I've successfully done this in classic HTML
before, but for some reason it doesn't seem like my text object on the
Main page is being referenced correctly. I'm not sure if there is a
step between .NET and javascript I am missing. In classic ASP, you
referenced HTML controls by name, but since that is no longer
available, I reference by clientID...is this a problem?

The code is as follows:

-----------
MAIN.ASPX
-----------
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="main.aspx.vb" Inherits="WebApplication1.main"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>main</title>
<script id="clientEventHandlersJS" language="javascript">
<!--

var dialogArguments;
function getValue (field){
dialogArguments = field;
open('child.aspx', 'popup', 'width=400,height=300,scrollbars=1')
}

//-->

</script>
</HEAD>
<body>
<form id="formMain" method="post" runat="server">
<P> </P>
<P>
<asp:Button id="buttonMain" runat="server" Text="Get
Input"></asp:Button></P>
<P>
<asp:TextBox id="textMain" runat="server"></asp:TextBox></P>
</form>
</body>
</HTML>

---------------
MAIN.ASPX.VB
---------------

Public Class main
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents button1 As System.Web.UI.WebControls.Button
Protected WithEvents firstName As
System.Web.UI.WebControls.TextBox
Protected WithEvents buttonMain As
System.Web.UI.WebControls.Button
Protected WithEvents textMain 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
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

buttonMain.Attributes.Add("onClick",
"getValue(document.getElementById('" & textMain.ClientID & "'))")

End Sub

End Class

------------
CHILD.ASPX
------------
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="child.aspx.vb" Inherits="WebApplication1.child"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>child</title>
<script id="clientEventHandlersJS" language="javascript">
<!--

function buttonChild_onclick(){
var doc = document.forms[0];
if (doc.textChild.value == ""){
alert("Email message is empty.");
return false;
}
else
{
//alert(doc.textChild.value);
window.opener.dialogArguments.value = this.textChild.value;
window.close()
}}

//-->
</script>
</HEAD>
<body>
<form id="formChild" method="post" runat="server">
<asp:Button id="buttonChild" style="Z-INDEX: 101; LEFT: 216px;
POSITION: absolute; TOP: 136px"
runat="server" Text="Submit"></asp:Button>
<asp:TextBox id="textChild" style="Z-INDEX: 102; LEFT: 48px;
POSITION: absolute; TOP: 136px"
runat="server"></asp:TextBox>
</form>
</body>
</HTML>


--------------
CHILD.ASPX.VB
--------------
Public Class child
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents button1 As System.Web.UI.WebControls.Button
Protected WithEvents buttonChild As
System.Web.UI.WebControls.Button
Protected WithEvents textChild 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
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

buttonChild.Attributes.Add("onClick", "return
buttonChild_onclick()")

End Sub

End Class
 
J

Jorge Ponte

Sorry, I've made a mistake

In the Child.aspx You dont need the script tag, beacuse you'll add it in run
time.
So child.aspx will be:

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="child.aspx.vb"
Inherits="PopUpSample.child"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>child</title>
<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="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">

</HEAD>
<body>
<form id="formChild" method="post" runat="server">
<asp:button id="buttonChild" style="Z-INDEX: 101; LEFT: 216px; POSITION:
absolute; TOP: 136px"
runat="server" Text="Submit"></asp:button><asp:textbox id="textChild"
style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 136px"
runat="server"></asp:textbox></form>
</body>
</HTML>


Jorge Ponte
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top