Jump to content

ASP.NET: Pulling ServerSide Variables Into ClientSide Code


kwilliams

Recommended Posts

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)

<%@ 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)ColoradoSAMPLE #2 NON-WORKING ASP.NET MASTERPAGE:MasterPage.master.vb

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 SubEnd Class

MasterPage.master

<%@ 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.jsLine 11: var currentState = "<% Response.Write(CurrentState) %>";<<<------THIS IS THE ERRORLine 12: // -->Line 13: </script>

Link to comment
Share on other sites

I'm not much of a VB expert, but it looks like you are declaring the CurrentState variable inside the scope of your Page_Load handler. So, once control has left the page load stage and entered into the render stage, "'CurrentState' is not declared."Try moving declaration of that variable outside the Page_Load and then set it in Page_Load.In C#

public partial MyMasterPage : MasterPage{	public string CurrentState;	protected void Page_Load(object sender, EventArgs e)	{		CurrentState = "Colorado";	}}

Link to comment
Share on other sites

Thanks for replying so quickly Jesh.When I moved the variable declaration outside the Page_Load handler, like this:

Partial Class MasterPage	Inherits System.Web.UI.MasterPage	Dim CurrentState As String = "Colorado"End Class

I now receive a different error:Compiler Error Message: BC30390: 'MasterPage.CurrentState' is not accessible in this context because it is 'Private'.Do I need to add some other code around the variable declaration to make this work?

Link to comment
Share on other sites

Yeah, you need to make it public.According to this page, in VB you do that like so:
Public CurrentState As String

Hi Jesh,I had just received that suggestion from someone in another forum, so I guess great minds *do* think alike:)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 SubEnd ClasstoPartial 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 SubEnd Classand thisvar currentState = "<% Response.Write(CurrentState) %>";tovar 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."Thanks again Jesh:)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...