Solved

ASP.Net and Javascript variables

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

9 Experts available now in Live!

Get 1:1 Help Now