Click here to Skip to main content
13,252,815 members (49,828 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 1 Nov 2005

Implementing state-full functionality behavior of custom object instances in ASP.NET

, 1 Nov 2005
Rate this:
Please Sign up or sign in to vote.
Providing state-full functionality of objects in ASP.NET.

UML model


I wouldn’t be able to write this article without Anastasia. You are wonderful, you are beautiful and you give me inspiration and new strengths. I love you.


When you have some business logic that have to be implemented as a class and presented in your program code via an object of the corresponding class, it very often becomes a bare necessity to save the state of this object during aspx page postbacks. Some times it becomes hard to find an easy and convenient solution for this issue because of the stateless nature of HTTP. Hope that this article will help you to find some ideas on how to implement this functionality. The following solutions are based on the Singleton pattern. In short Singleton pattern allows you to manipulate objects (call its properties, methods) and guarantees that there is only one instance of this object presented at runtime.

Using the code

Solution I

This solution forces you to implement two static methods:

// returns singeltone instance from current page Session
public static ShonnCapushonClass GetSingletoneInstance (Page page)
 if (page.Session[ShonnCapushonClass.SessionId] != null)
  return (ShonnCapushonClass)page.Session[ShonnCapushonClass.SessionId];
  page.Session[ShonnCapushonClass.SessionId] = new ShonnCapushonClass ();
  return (ShonnCapushonClass)page.Session[ShonnCapushonClass.SessionId];


public static void SetSingletoneInstance (Page page, 
                        ShonnCapushonClass instance)
  page.Session[ShonnCapushonClass.SessionId] = instance;

In the aspx page code-behind, it will look like:

ShonnCapushonClass ShonnaObject = 
ShonnaObject.Name = "Tusenna";

When you do not need this object anymore, remove it from the session by calling:

ShonnCapushonClass.SetSingletoneInstance(this, null);

Solution II

This solution will be helpful when all actions of the object is encapsulated in a single aspx page. Let’s define a page property like this:

private ShonnCapushonClass PageShonnaObject
    return ShonnCapushonClass.GetSingletoneInstance(this);
    ShonnCapushonClass.SetSingletoneInstance(this, value);

In this case, manipulations can be easily done by referencing to the page property:

PageShonnaObject.Name = "PageTysena";
LabelShonnaName.Text = PageShonnaObject.Name;

When you need to reset the object state, just execute the following code:

PageShonnaObject = null;

Solution III

This is the most flexible solution that can be provided without the page instance and in this case your business object will be accessed from code until you explicitly destroy it or the user’s session expires. Also your object will be accessed from any page in your Web application: let’s define the Current property of your class like that:

public static ShonnCapushonClass Current
   if ( HttpContext.Current.Session[ShonnCapushonClass.SessionId] == null )
     ShonnCapushonClass shonnCapushon = new ShonnCapushonClass();
     HttpContext.Current.Session[ShonnCapushonClass.SessionId] = shonnCapushon;
     return shonnCapushon;
     return (ShonnCapushonClass)
   HttpContext.Current.Session[ShonnCapushonClass.SessionId] = value;

After that your business object can be at any page of your application by implementing calls like:

ShonnCapushonClass.Current.Name = "CurrentTysena";


ShonnCapushonClass.Current = null;


In this article I’ve shared some ideas about managing business object state between aspx page postbacks and described some solutions for bypassing the constraint of the stateless nature of HTTP using session state and the Singleton pattern.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alex Getman
Web Developer
Ukraine Ukraine

You may also be interested in...

Comments and Discussions

GeneralMore photo needed Pin
Pascal Ganaye2-Nov-05 2:21
memberPascal Ganaye2-Nov-05 2:21 
GeneralRe: More photo needed Pin
mbaskey2-Nov-05 8:31
membermbaskey2-Nov-05 8:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 1 Nov 2005
Article Copyright 2005 by Alex Getman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid