Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can SCRIPT defined var be referenced in code behind?

Posted on 2009-05-04
3
Medium Priority
?
196 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

876 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