Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

ASP.Net and Javascript variables

Here is my code:
---
<%@ Page Language="VB" %>
<Script Runat="server">            
      Sub Page_load()
            Dim strCity As String
            Dim strState As String
            Dim intZip As Integer
            
            strCity = "Tampa"
            strState = "Fl"
            intZip = 33607
      
            lblCity.Text = strCity
            lblState.Text = strState
            lblZip.Text = intZip
      End Sub
</Script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>window_page1.aspx</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
      leftPos = 0
      if (screen) {
            leftPos = screen.width-750
      }
      
      function OpenWindow()
      {
       window.open("window_page2.aspx?City=<%= strCity %>&State=<%= strState %>&Zip=<%= intZip %>", "newWin", "width=550,height=500,left="+leftPos+",top=0,statusbar=yes,resizable=yes,scrollbars=yes,status=yes")
      }
</SCRIPT>
</head>
<body>
<a href="JavaScript:OpenWindow()">Open Window</a>
<p>
<b>Variables to be passed:</b><br>
City: <asp:label id="lblCity" runat="server" Text=""/>
<br />
State: <asp:label id="lblState" runat="server" Text=""/>
<br />
Zip: <asp:label id="lblZip" runat="server" Text=""/>
<br />
</body>
</html>
---

When I try and run this I get the following error:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30451: Name 'intZip' is not declared.
Source Error: Line 31:        window.open("window_page2.aspx?City=<%= strCity %>&State=<%= strState %>&Zip=<%= intZip %>", "newWin", "width=550,height=500,left="+leftPos+",top=0,statusbar=yes,resizable=yes,scrollbars=yes,status=yes")


Any ideas?
0
matthersjr
Asked:
matthersjr
1 Solution
 
AerosSagaCommented:
try it this way:
window.open("window_page2.aspx?City=<%= Me.lblcity.text %>&State=<%= Me.lblState.text %>&Zip=<%= Me.lblZip.Text %>", "newWin", "width=550,height=500,left="+leftPos+",top=0,statusbar=yes,resizable=yes,scrollbars=yes,status=yes")

Regards,

Aeros
0
 
hismightinessCommented:
Have you tried using literal controls instread of the Response.Write?  It looks like the code runs, then the page is rendering, but in the mean time the variables have already been processed in a past event (page load), and the variable scope is lost.  You could either use literal controls, or even better have a couple of functions written to return your variables and call them like this:

<%@ Page Language="VB" %>
<Script Runat="server">          
     Dim strCity As String
     Dim strState As String
     Dim intZip As Integer

     Sub Page_load()
          strCity = "Tampa"
          strState = "Fl"
          intZip = 33607
     
          lblCity.Text = strCity
          lblState.Text = strState
          lblZip.Text = intZip
     End Sub

     Public Function rtnCity() as String
          return strCity
     End Function

     Public Function rtnState() as String
          return strState
     End Function

     Public Function rtnZip() as String
          return CType(strZip, String)
     End Function
</Script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>window_page1.aspx</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
     leftPos = 0
     if (screen) {
          leftPos = screen.width-750
     }
     
     function OpenWindow()
     {
      window.open("window_page2.aspx?City=<%= rtnCity() %>&State=<%= rtnState() %>&Zip=<%= rtnZip() %>", "newWin", "width=550,height=500,left="+leftPos+",top=0,statusbar=yes,resizable=yes,scrollbars=yes,status=yes")
     }
</SCRIPT>
</head>
<body>
<a href="JavaScript:OpenWindow()">Open Window</a>
<p>
<b>Variables to be passed:</b><br>
City: <asp:label id="lblCity" runat="server" Text=""/>
<br />
State: <asp:label id="lblState" runat="server" Text=""/>
<br />
Zip: <asp:label id="lblZip" runat="server" Text=""/>
<br />
</body>
</html>
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now