Solved

Can SCRIPT defined var be referenced in code behind?

Posted on 2009-05-04
3
186 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

13 Experts available now in Live!

Get 1:1 Help Now