473,698 Members | 2,371 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 1580
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
1919
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
2053
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
1490
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
1903
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
2666
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
9279
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
3169
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
3576
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
1631
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
8683
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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
5862
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
4371
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
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.