473,739 Members | 7,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static fields in ASP.NET pages

Hello

I'm writing an application in ASP.NET 1.1 and have come across a
problem using static fields in my page classes.

I have lots of controls on the page that all need to bind to the same
datasource, so I thought I'd create a singleton in the base class that
reads from the database and provides a DatasSet to all instances of the
various derived classes. I've discovered, though, that the static
values persist between roundtrips, presumably because the singleton
instance hasn't been released by the garbage collector. For this
reason, I've decided that using static in my ASP.NET pages is probably
unsuitable.

Does anyone have any recommendations for using static members in
ASP.NET, or is there a more appropriate method for implementing
singletons in ASP.NET?

Thanks a lot, Mark

Mar 23 '06 #1
3 1584
Mark,

Note, that static members persist not only between roundtrips of the same
session, but also between different sessions of the same application. This
means that multiple users working in the same time will be working on the
same objects.

Eliyahu

<ma**********@g mail.com> wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.com.. .
Hello

I'm writing an application in ASP.NET 1.1 and have come across a
problem using static fields in my page classes.

I have lots of controls on the page that all need to bind to the same
datasource, so I thought I'd create a singleton in the base class that
reads from the database and provides a DatasSet to all instances of the
various derived classes. I've discovered, though, that the static
values persist between roundtrips, presumably because the singleton
instance hasn't been released by the garbage collector. For this
reason, I've decided that using static in my ASP.NET pages is probably
unsuitable.

Does anyone have any recommendations for using static members in
ASP.NET, or is there a more appropriate method for implementing
singletons in ASP.NET?

Thanks a lot, Mark

Mar 23 '06 #2
> Hello

I'm writing an application in ASP.NET 1.1 and have come across a
problem using static fields in my page classes.

I have lots of controls on the page that all need to bind to the same
datasource, so I thought I'd create a singleton in the base class that
reads from the database and provides a DatasSet to all instances of the
various derived classes. I've discovered, though, that the static
values persist between roundtrips, presumably because the singleton
instance hasn't been released by the garbage collector. For this
reason, I've decided that using static in my ASP.NET pages is probably
unsuitable.

Does anyone have any recommendations for using static members in
ASP.NET, or is there a more appropriate method for implementing
singletons in ASP.NET?

Thanks a lot, Mark


A singleton should not be "released by the garbage collector". It
should remain alive for the duretion of the application.
An asp.net application is a single application that keeps on running
until you explicitly stop it. During this lifetime it serves many
sessions consisting of many requests.

It is possible to use static members and singletons in asp.net, but you
need to know the effects: *all* users will the use the *same*
instances. If that is what you want, fine.

What you may want is static *methods*. You don't need to instantiate
the class, you can (or rather: need to) call these on the *type*.
Example:

public sealed class MyLibrary
{
private MyLibrary() { } // no public constructor needed

public static DataSet GetData(int id)
{
// ....
return ..;
}
}

and you call that with:

DataSet ds = MyLibrary.GetDa ta(1);

Hans Kesting
Mar 23 '06 #3
Thanks Hans. Perhaps I ought to be a little more explicit with my
requirements.

My intention is that the singleton instance should be alive for the
duration of the request, on a per-user basis. When the page goes out of
scope, the singleton should be destroyed; or, more precisely, the data
should be reloaded when the page is loaded afresh by the server in
response to a fresh request (i.e., not a postback).

There are two problems: the singleton instance is shared across
threads, but I suspect I may be able to deal with that using the
ThreadStaticAtt ribute attribute; more importantly is the fact that the
singleton is surviving roundtrips to the server. If I implement
ThreadStaticAtt ribute then perhaps this won't be the case. This leads
to another question: is a thread created when a request is made of the
server, and does it end when the request has been fulfilled, or does it
end when the session goes out of scope?

As for your static method suggestion, what effect does marking it as
static have? Presumably the function will be executed every time it is
called, so that will defeat the whole reason I wanted a singleton in
the first place: to make sure the data is only loaded once from the
database per visit to the page (including postbacks).

Aargh!

Mark

Mar 23 '06 #4

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

Similar topics

5
1920
by: Mountain Bikn' Guy | last post by:
How would I do this? public sealed class UtilityClass { public static MyObject Object1;//see note below about importance of static object names in this class public static MyObject Object2; // ... public static MyObject Object400;
8
2056
by: Fernando Lopes | last post by:
Hi there! Someone has some code sample about when is recommend use a statis method? I know this methos don't want to be initialized and all but I want to know when I need to use it. Tks. Fernando
3
1493
by: Alexander | last post by:
Hi all I has Component inside ASP.NET page that create CO object and store reference to it in the Component's privat static field. Then I cast this reference to desired interfac and call interface method. All works fine.(No exception throwed Then I push button and the same ASP.NET page initiated again Components private static field has reference to previously create COM object. But when I try to cast this reference to COM interfac
6
1907
by: Victor Hadianto | last post by:
Hi All, I'm a beginner with ASP.Net so please bear with me. I'm having difficulties trying to understand the behaviour of static variable across multiple ASP.Net page. My static variable is a Hashtable and located in a dll which is referenced by my ASP.Net aspx pages. In one pages I have a form and in the form submit I remove some items from the Hashtable depending on user's data. On another button in this form I do a...
9
2669
by: Tim_Mac | last post by:
hi, i'm not sure if i have chosen the best approach, but it seemed quite good to me. i have a collection class, containing business objects. the collection class is static and remains in-memory all the time the app is running. it is persisted to an Xml file when necessary. i keep a static filestream on the xml file. the advantage of this is that i can lock the filestream object to prevent concurrent reading/writing. the code works...
2
9284
by: utab | last post by:
Dear all, I am confused at some point on the initialization of static map member of a class. The class I have designed id something like this. class Class_name{ public: private:
5
3176
by: pittendrigh | last post by:
There must be millions of dynamically generated html pages out there now, built by on-the-fly php code (and jsp, perl cgi, asp, etc). Programatic page generation is transparently useful. But querying a database, negotiatinig lots of if-then-else logic and echo'ing html code out on port 80 every time a page is requested has to be a huge waste of resources. Why not use that logic to print static html instead of dynamic?
12
3581
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
2
1634
by: BLUE | last post by:
FirstClass members: - static int counter; - SingletonClass sc = SingletonClass.Instance; Moreovere FirstClass uses a static class named SecondClass with a static property SecondClass.Description. In a normal windows application: - if in the Main method I create 2 or more instances of FirstClass, they all
0
9479
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9209
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6054
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3280
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.