Click here to Skip to main content
12,550,731 members (31,318 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked

ObjectContainer - A simple way to demarcate SessionState between pages

, 9 Feb 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes how to create a simple WebControl to isolate part of SessionState for a page or control to store data.


I once developed a new Web Application, and the problem of storing some page-related data was raised. These were the main problems:

  • Can't save data to the database. My objects can be changed, and changes should be committed later (something like a wizard).
  • Can't put objects to the ViewState due to the huge size.
  • Can't use Session because the user can work with more than one page copies at the same time.
  • Data access layer doesn't support caching mechanism.
  • There are no special state machines.

The problem was resolved by writing a simple Web Control which holds the anchor to the data container where I store my objects.


We have only two properties and two fields in our custom Web Control derived from the WebControl class. First one:

private Guid _Key = Guid.Empty;
private Guid Key
        if ( _Key == Guid.Empty )
            if ( this.ViewState[this.UniqueID] != null )
                _Key = (Guid) this.ViewState[this.UniqueID];
                this.Key = Guid.NewGuid();
        return _Key;
        _Key = value;
        this.ViewState[this.UniqueID] = _Key;

This is just the anchor for our data storage. We insert the anchor in the ViewState. The second:

private Hashtable _Data;
public Hashtable Data
        if (_Data == null )
            if ( HttpContext.Current.Session[this.Key.ToString ()] != null )
                _Data = ( Hashtable ) HttpContext.Current.Session[this.Key.ToString()];
                this.Data = new Hashtable ();
        return _Data;
    private set
        _Data = value;
        HttpContext.Current.Session[this.Key.ToString ()] = _Data;

As we can see, the public Hashtable uses a key to set and get the anchored data to and from the session.

As a result, we have a Web Control which can be easily drag 'n' dropped to your page or control, and then you can use the Data property to store your objects without caring about data identity in page copies at runtime.


Of course, the main problem of this control is memory usage. All objects will be in memory till the session is alive. To prevent memory overload, the session should be cleaned manually when you finish all your operations with the container. Like that:

public void Clear ()
    HttpContext.Current.Session.Remove ( this.Key.ToString () );

Next time, I will describe how to extend this idea and implement a session container without using custom web controls.

Revision History

  • Added examples for .NET 1.1 and 2.0.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author
Web Developer
Belarus Belarus
Graduated in Belurassian State University on chair of Artificial Intelligence.
Take a part in development video alarm system for PC with live AI analyzing multiple video streams.

3 years of ASP.NET experience in development multi-thier applications, integrated applications, enterprise level solutions.

Almost 1 year experience in ASP.NET 2.0 starting from BETA 2.

Now working in

You may also be interested in...


Comments and Discussions

GeneralPossible Improvement Pin
Adam L. Stevenson15-Feb-06 19:01
memberAdam L. Stevenson15-Feb-06 19:01 
GeneralRe: Possible Improvement Pin
Dzianis@gmail.com26-Feb-06 21:58
memberDzianis@gmail.com26-Feb-06 21:58 
QuestionHmm. One question Pin
dj_uncas9-Feb-06 6:47
memberdj_uncas9-Feb-06 6:47 
AnswerRe: Hmm. One question Pin
Dzianis@gmail.com26-Feb-06 21:49
memberDzianis@gmail.com26-Feb-06 21:49 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 9 Feb 2006
Article Copyright 2006 by
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid