Solved

ASP.Net and Javascript variables

Posted on 2004-09-17
2
261 Views
Last Modified: 2010-05-18
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
Comment
Question by:matthersjr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 12088549
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
 
LVL 9

Expert Comment

by:hismightiness
ID: 12088579
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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