Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 5th, 2008, 11:42 AM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pulling ServerSide Variables Into ClientSide Code

I'm trying to pull a server-side variable (VB) into client-side code (JavaScript). I can do this successfully using a regular ASP.NET page, but when I try to implement the same thing using the MasterPage method, I receive an error. I've included the working and non-working code below, along with the results of each. If anyone can please let me know what I'm doing wrong, that would be greatly appreciated. Thanks.

SAMPLE #1: WORKING CODE (SINGLE ASP.NET PAGE)

Code:
<%@ Page Language="VB" Explicit="True" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<script runat="server">
    'Like MasterPage.master.vb
    Dim CurrentState As String = "Colorado"
</script>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Current State</title>
<script language="JavaScript">
<!--
    //Like MasterPage.js
    var currentState = "<% Response.Write(CurrentState) %>";
// -->
</script>
</head>
<script language="JavaScript">
    //Like MasterPage.master
    document.write(currentState);
</script>
</body>
</html>
SAMPLE #1 RESULT: (works properly)
Colorado


SAMPLE #2 NON-WORKING ASP.NET MASTERPAGE:
MasterPage.master.vb
Code:
Partial Class MasterPage
    Inherits System.Web.UI.MasterPage
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim CurrentState As String = "Colorado"
    End Sub
End Class
MasterPage.master
Code:
<%@ Master Language="VB" CodeFile="MasterPageAlerts.master.vb" Inherits="MasterPageAlerts" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script language="JavaScript">
        <!--
        //Like MasterPage.js
        var currentState = "<% Response.Write(CurrentState) %>";
        // -->
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <script language="JavaScript">
    //Like MasterPage.master
    document.write(currentState);
</script>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>
SAMPLE #2 RESULT: (error)
Compiler Error Message: BC30451: Name 'CurrentState' is not declared.
Line 9: <!--
Line 10: //Like MasterPage.js
Line 11: var currentState = "<% Response.Write(CurrentState) %>";<<<------THIS IS THE ERROR
Line 12: // -->
Line 13: </script>

KWilliams
  #2 (permalink)  
Old February 5th, 2008, 02:39 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Change this:
Partial Class MasterPage
    Inherits System.Web.UI.MasterPage
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim CurrentState As String = "Colorado"
    End Sub
End Class

to

Partial Class MasterPage
    Inherits System.Web.UI.MasterPage
    Public CurrentState as String
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        CurrentState = "Colorado"
    End Sub
End Class

and this
var currentState = "<% Response.Write(CurrentState) %>";

to
var currentState = "<%= CurrentState %>";

hth

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
  #3 (permalink)  
Old February 5th, 2008, 02:41 PM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

I probably should explain why you need to do that! >< When you declare your variable CurrentState inside of your method like that, it is in scope only to that method and to nothing else. By declaring it as a Public member of the class it is aviable to all methods in your class and thus allows you to use the value on your page.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
================================================== =========
  #4 (permalink)  
Old February 6th, 2008, 04:57 PM
Banned
Points: 1,561, Level: 16
Points: 1,561, Level: 16 Points: 1,561, Level: 16 Points: 1,561, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2005
Location: , , .
Posts: 317
Thanks: 0
Thanked 0 Times in 0 Posts
Default

dparsons,

It worked like a charm. Thanks for the solution to what I thought would be a never-ending problem, and thanks for helping me to understand why and how it works in the first place. I REALLY appreciate that. Thanks.:)

KWilliams


Similar Threads
Thread Thread Starter Forum Replies Last Post
serverside ajax language david_kw Ajax 5 May 28th, 2007 11:41 AM
SERVERSIDE & CLIENTSIDE APPLICATION u_roc100@rediffmail.com Forum and Wrox.com Feedback 1 October 9th, 2005 10:29 AM
Global Variables and ClientSide Javascript haricot Classic ASP Basics 3 September 7th, 2005 10:00 AM
how to validate a date clientside lcsgeek Classic ASP Professional 2 October 3rd, 2003 03:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.