473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.Page
{

static public string sharedvar;

protected void Page_Load(object sender, EventArgs e)
{
...

thanks

Apr 17 '07 #1
5 2683
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.MyItemName;

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.Page
{

static public string sharedvar;

protected void Page_Load(object 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*******@yahoo.yabbadabbadoo.comwrote in
message news:5E**********************************@microsof t.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.MyItemName;

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.Page
{

static public string sharedvar;

protected void Page_Load(object 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*********************@b58g2000hsg.googlegro ups.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.Page
{

static public string sharedvar;

protected void Page_Load(object 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
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
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...
3
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...
10
by: Suresh | last post by:
how to share Session Sharing Across the domain
4
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...
2
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...
2
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,...
8
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.