www.webdeveloper.com
Results 1 to 2 of 2

Thread: ASP.NET: Pulling ServerSide Variables Into ClientSide Code

  1. #1
    Join Date
    May 2003
    Posts
    599

    Question ASP.NET: 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
    -----------------------
    It's the end of the world as we know it...and I feel fine

  2. #2
    Join Date
    May 2003
    Posts
    599

    resolved

    Here is the complete solution:

    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 %>";

    "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."
    KWilliams
    -----------------------
    It's the end of the world as we know it...and I feel fine

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles