Solved

ASP.Net and Javascript variables

Posted on 2004-09-17
2
256 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now