Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

Global Variable

  Asked By: Gerritt    Date: Jul 22    Category: Asp.net    Views: 2125
  

How to declare a global Variable in ASP.net Application, So that it Should be
static variable and should not be initialized at all times each page inherits
it. Also how this variable accessed by all pages in Application.

Share: 

 

9 Answers Found

 
Answer #1    Answered By: Bradley Evans     Answered On: Jul 22

You can maintain the value of a variable  within a
session (ie till the user logs out of your web
application ), which can be maintained between pages.
Session state can be memory based or could be in a
database.As such, 'global variables' are not supported
in ASP.Net.

 
Answer #2    Answered By: Barak Levi     Answered On: Jul 22

There is a project sln at http://www.developa.com/webapplication123.zip

It shows how to use ascx and custom controls.

And if you think about it, using the methods therein you could feasibly use one
of these controls to hold all your global  variables accessing them at will.

 
Answer #3    Answered By: Rosalie Holmes     Answered On: Jul 22

Or just use a session variable

Session["hello"] = object.

 
Answer #4    Answered By: Zachary Larson     Answered On: Jul 22

Oh no, sorry, that's WinForms!

 
Answer #5    Answered By: Russell Burns     Answered On: Jul 22

No, I am right ..... the original question said static vars.

 
Answer #6    Answered By: Louis Mason     Answered On: Jul 22

This .net thing also supports global  static variables in the global.asax
file. You can do this by...


--- global.asax ---
<%@ application  Codebehind="Global.asax.cs" Inherits="TestVBNet.Global"
Classname="CommerceApplication" %>
<Script Language="C#" runat=server>
public static string strPublicString = "yo yo3";
</Script>

--- pagethatwillusetheglobalstaticvariabe.aspx ---
< %@Import <mailto:%@Import> Namespace="System"%>
<%@ page  Language="vb" AutoEventWireup="false" %>
<HTML>
<HEAD>
<SCRIPT runat=server>

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.Write(CommerceApplication.strPublicString)
End Sub
</SCRIPT>
</HEAD>
<body>
<form id=frmX runat=server></form>
</body>
</HTML>


You can access the variable  by <ClassName>.<VariableName> -- where
<ClassName> is the value of the Classname attribute in the Application tag
in global.asax. You can also modify that variable - but i think it'll only
be initialized once when the application loads.

 
Answer #7    Answered By: Hehet Chalthoum     Answered On: Jul 22

Why not just use an Application variable. You can access it
from all your pages. Just declare  it in your Application_OnStart
event: Application["ImportantMessage"] = "Whoopee!" and
use it wherever you choose.

 
Answer #8    Answered By: Sean Grant     Answered On: Jul 22

The original question said they didn't want to use sessions ! and that the vars
were static

 
Answer #9    Answered By: Huette Miller     Answered On: Jul 22

Declare a Public Shared Variable in the Global.asax file.

You can access it via Global.YourSharedMember

 
Didn't find what you were looking for? Find more on Global Variable Or get search suggestion and latest updates.




Tagged: