Solved

Can SCRIPT defined var be referenced in code behind?

Posted on 2009-05-04
3
187 Views
Last Modified: 2012-05-06
If I define a variable as below, which is global, is there a way to reference it in code behind?

Is this variable global only to the particular page class it is defined in?  If that is the case, why can't it be referenced in code behind as defined below?

Is there any MSDN documentation on this particular scenario?
...

<title></title>

</head>

<SCRIPT Runat="Server">

string MyVar;

</SCRIPT>

<body>

<form id="form1" runat="server">

<div>
 

<% MyVar = " A Test"; %>

...

Open in new window

0
Comment
Question by:brettr
  • 2
3 Comments
 
LVL 8

Expert Comment

by:ppittle
ID: 24296442
Brettr,

What you've run into is more or less by design.  When the asp.net compiler converts your declarative markup into classes that the CLR will work with, it creates two classes per page:  One class for the declarative markup, and one class for the code behind.  The delcarative markup class inherits from the code behind.  This is why if you specify an event handler in your declarative markup, it can't be declared private.  

Because Microsoft expected developers to need to progromatically work with Controls that have been declared declaritively, Visual Studio automatically adds definitions for all of your controls into your code behind class.  In .NET 1.1 this was in the form of a desinger section.  In 2.0 and beyond, it's placed in a seperate file (aspx.designer.cs or aspx.designer.vb). However, this is limited to Controls.  So if you add variables in script elements, they wont be added to your code-behind and you wont be able to access them from your code behind.  For it to work, we'll need to manually add it ourselves.

Once we understand how asp.net is creating classes out of your declartive markup, we can modify your solution above by declaring your variable in the code behind.  Then we can access it from both declarative markup, and code behind:

<html>
<body>
<MyVar="Hello World!!!!"; Response.Write(MyVar); %>

<%-- Code Behind --%>
public partial class MyPage : System.Web.UI.Page
{
protected string MyVar = string.Empty;
}

To see an example of this inheritence in action, change the definition of MyVar in your code-behind to:
private string MyVar = string.Empty;

PJ
0
 

Author Comment

by:brettr
ID: 24299734
What is the inheriting class name in the aspx?  Does Microsoft document how that is derived?  The code behind class usually gets an underscore prepended to its name.  For example, the aspx references:

Inherits="MyNameSpace._MyPage" %>
0
 
LVL 8

Accepted Solution

by:
ppittle earned 500 total points
ID: 24299915
aspx pages get converted to classes, and are given class names, during dynamic compilation time (when the resource is first requested) or during compile time, if the precompile option is sent to the aspnet compiler.  The code-behind class uses the class named specified in the class definition.  (From your example, the code behind class would be MyNameSpace._MyPage).

Here are some additional resources on how an aspx site gets compiled:

ASP.NET Comiplation Tool - http://msdn.microsoft.com/en-us/library/ms229863(vs.80).aspx
ASP.NET Dynamic Compilation - http://msdn.microsoft.com/en-us/library/ms366723(VS.80).aspx 

Finally, if you're looking for a way to have a 'global' variable you can access from the code-behind and declarative markup of ALL of your pages, you can use a static class:

public static class GlobalVariables
{
public static string MyVar = string.Empty;
}

You can access GloblVariables.MyVar anywhere in your code.  Note:  This varialbe will not persist across post backs.  If you need it to persist across postbacks, try this:

public static class GlobalVariables
{
public static string MyVar
{
get{
object o = ViewState["MyVar"];
if (null == o){ ViewState["MyVar"] = string.Empty;}
return (string)ViewState["MyVar"];
}
set{ ViewState["MyVar"] = value; }
}
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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

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

19 Experts available now in Live!

Get 1:1 Help Now