Solved

Can SCRIPT defined var be referenced in code behind?

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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