ASP.NET - VB.NET - JavaScript Question

Discussion in 'ASP .Net Web Controls' started by Thomas, Dec 3, 2004.

  1. Thomas

    Thomas Guest

    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
     
    Thomas, Dec 3, 2004
    #1
    1. Advertising

  2. Thomas

    Jorge Ponte Guest

    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" wrote:

    > 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
    >
     
    Jorge Ponte, Dec 19, 2004
    #2
    1. Advertising

  3. Thomas

    Jorge Ponte Guest

    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
     
    Jorge Ponte, Dec 19, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Voigt
    Replies:
    2
    Views:
    1,038
    Jeff Voigt
    Aug 8, 2003
  2. David Virgil Hobbs
    Replies:
    0
    Views:
    533
    David Virgil Hobbs
    Jan 6, 2004
  3. Roy
    Replies:
    2
    Views:
    439
  4. Roy
    Replies:
    2
    Views:
    421
    Joe Fallon
    Jun 4, 2005
  5. Joey
    Replies:
    4
    Views:
    493
    sloan
    Feb 2, 2006
Loading...

Share This Page