473,546 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sharing a variable between all pages

Hi,
New to .NET, I was wondering what was the proper way to share a
variable between all the instances of a web page.
I try with a static var, but it does not seem to always work. Why is
that ?
public partial class _Default : System.Web.UI.P age
{

static public string sharedvar;

protected void Page_Load(objec t sender, EventArgs e)
{
...

thanks

Apr 17 '07 #1
5 2699
When you say "does not seem to always work" - what happens?

Either way, a static field is a risky proposition in a highly threaded
environment such as ASP.Net; at the very least this should be
protected by a property and synchronised between threads. There may be
more appropriate solutions: what are you trying to do with this var?
What does it hold?

Marc

Apr 17 '07 #2
This is probably more of an ASP.NET question than a C# language group one.
Typically in ASP.NET you would store your item either in Session,
Application, or Cache state depending on the business scenario or whether it
is user-specific.
Alternatively, you can put a public static field in your Global class
(global.asax) and you can then use it from any page with:

Global.MyItemNa me;

Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"ol**@linuxmail .org" wrote:
Hi,
New to .NET, I was wondering what was the proper way to share a
variable between all the instances of a web page.
I try with a static var, but it does not seem to always work. Why is
that ?
public partial class _Default : System.Web.UI.P age
{

static public string sharedvar;

protected void Page_Load(objec t sender, EventArgs e)
{
...

thanks

Apr 17 '07 #3
Yeah. The thing is, the Session is more for maintaining state across
different pages associated with the same Session. Perhaps the OP could
explain if these pages are sharing a Session - or if they are in different
Sessions (i.e. a second instance in a second browser instance on the same
client - or even two separate browser instances on different machines with
potentially different users). Until the OP gives us a bit more info about
what is required, it's hard to know what the exact nature of the problem is.
Peter
"Peter Bromberg [C# MVP]" <pb*******@yaho o.yabbadabbadoo .comwrote in
message news:5E******** *************** ***********@mic rosoft.com...
This is probably more of an ASP.NET question than a C# language group one.
Typically in ASP.NET you would store your item either in Session,
Application, or Cache state depending on the business scenario or whether
it
is user-specific.
Alternatively, you can put a public static field in your Global class
(global.asax) and you can then use it from any page with:

Global.MyItemNa me;

Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"ol**@linuxmail .org" wrote:
>Hi,
New to .NET, I was wondering what was the proper way to share a
variable between all the instances of a web page.
I try with a static var, but it does not seem to always work. Why is
that ?
public partial class _Default : System.Web.UI.P age
{

static public string sharedvar;

protected void Page_Load(objec t sender, EventArgs e)
{
...

thanks


Apr 17 '07 #4
could you provide sample code for storing a var at an application
scope ? (not session scope)

(by "it does not seem to always work", I mean that the static var has
been set by some instance of the page, and another read a null value -
this behavior is different from java as to me)

Apr 17 '07 #5
Sounds like the Singleton pattern is in order here. Google 'Singleton
pattern in C#' and you should find countless examples of the middle tier
code required to implement the concept of global variables in a web or
windows form applcation.
<ol**@linuxmail .orgwrote in message
news:11******** *************@b 58g2000hsg.goog legroups.com...
Hi,
New to .NET, I was wondering what was the proper way to share a
variable between all the instances of a web page.
I try with a static var, but it does not seem to always work. Why is
that ?
public partial class _Default : System.Web.UI.P age
{

static public string sharedvar;

protected void Page_Load(objec t sender, EventArgs e)
{
...

thanks

Apr 17 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1533
by: Atif Jalal | last post by:
How can I share the sane databse connection between asp.net pages. I also use objects(middleware), so can I pass the same DB connection when I instantiate the object by calling its constructor?
6
1138
by: Yama | last post by:
Hello All I have developed a new site using ASP.NET/VB.NET. I fell into a big problem. Here is the problem I created a module as follow Namespace FinancialCompany.Porta Public Class myPropertie Private _intCounter As System.Int3
3
2780
by: Shikari Shambu | last post by:
Hi All, I have a situation where multiple applications are sharing some pages/ controls. So, I have a separate common project that has the common pages/ controls. My question is how do I reference these pages/ controls from my ASP.NET web projects WEbApp1 url http://localhost/app1 C:\Apps\App1
10
2201
by: Suresh | last post by:
how to share Session Sharing Across the domain
4
1749
by: abcd | last post by:
I have an ASP application. It instantiaties some COM components and we put those COM components in Session variables...COM components have license restrictions...We have written new ASPX page ( for better GUI and faster development) and we will be using that ASPX page along with our ASP application...how can I pass that Session variable (which...
2
1548
by: Godot | last post by:
I have an ASP.NET 1.1 web app that stores a current record ID value in a session variable so it can be accessed from multiple pages for inserting/updating of related records. Unfortunately, if the user chooses FILE/NEW/WINDOW in IE, the new browser window has the same session ID as the original browser window. Therefore, if the user...
2
2739
by: Chris Fink | last post by:
What is the recommendation for sharing master pages across IIS Servers? In our corporation, we have many webservers in different environments (intranet, extranet, etc). I've done some searching, and it appears as though the solutions are focused only around one IIS instance, by installing the controls (master pages) in the GAC. This...
8
2407
by: antonyliu2002 | last post by:
We are extending a web application written in classic ASP long time ago. We will add more components to this web application in ASP.NET 2.0. To use the web application, our web users will have to log in with their user name and password. Well, instead of adding components to the existing classic ASP web application, we could have just...
5
1217
by: Carlos | last post by:
Hi all, what is the best way to share the data of lets say a textbox text between pages? for example if I fill the textbox in one page and later redirect to a new page, and I would like to have that information there as well (VB). Thanks in advance. Carlos.
0
7504
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7792
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.