Click here to Skip to main content
13,903,354 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


15 bookmarked
Posted 29 May 2010
Licenced CPOL

Observable Collections

, 3 Jun 2010
Rate this:
Please Sign up or sign in to vote.
A small test program to illustrate how the ObservableCollection works, with special notes on using it in Asp.Net.

I didn't think it was possible, but .NET surprised me yet again with a cool feature I never knew existed: The ObservableCollection. This became available in .NET 3.0.

Note: This is about ObservableCollections as they pertain to ASP.NET.

In essence, an ObservableCollection is a collection with an event you can connect to. The event fires when the collection changes. As usual, working with the .NET classes is so ridiculously easy, it feels like cheating.

However, using the ObservableCollection, or any other object with an event handler, can be tricky in Asp.Net. Asp.Net member RichardD pointed out some errors in my original post and I would like to thank him for his comments. He also showed me a better way to use HttpContext.Current.Handler which simplified the code. However, since his comments were no longer relevant after the rewrite I deleted them…sorry buddy.

The following is a small test program to illustrate how the ObservableCollection works. To start, create an ObservableCollection and then store it in the Session object so it will persist between page post backs. I also added the code to pull it out of Session state when there is a page post back:

public partial class _Default : System.Web.UI.Page
    public ObservableCollection<int> MyInts;

    // ---- Page_Load ------------------------------

    protected void Page_Load(object sender, EventArgs e)
        if (IsPostBack == false)
            MyInts = new ObservableCollection<int>();
            MyInts.CollectionChanged += CollectionChangedHandler;

            Session["MyInts"] = MyInts;  // store for use between postbacks
            MyInts = Session["MyInts"] as ObservableCollection<int>;

Here's the event handler I hooked up to the ObservableCollection, it writes status strings to a ListBox. Note: the event handler is a static method.

// ---- CollectionChangedHandler -----------------------------------
// Something changed in the Observable collection

static public void CollectionChangedHandler
    (object sender, NotifyCollectionChangedEventArgs e)
    _Default CurrentPage = System.Web.HttpContext.Current.Handler;
    switch (e.Action)
        case NotifyCollectionChangedAction.Add:
            CurrentPage.ListBoxHistory.Items.Add("Add: " + e.NewItems[0]);               
        case NotifyCollectionChangedAction.Remove:
            CurrentPage.ListBoxHistory.Items.Add("Remove: " + e.OldItems[0]);
        case NotifyCollectionChangedAction.Reset:
            CurrentPage.ListBoxHistory.Items.Add("Reset: ");

In the original post, I had the event handler as a normal member function of the page. This caused two problems:

  • Since the ObservableCollection was put in Session state, and it had a reference to the page object, the initial page object was NOT released for garbage collection. I verified this by putting trace statements in the page’s constructor and destructor and verified the initial page object persisted. On subsequent post backs the page objects were created, released and discarded correctly. The initial page object remained in memory...not too big a deal but it just ain't right.
  • A member function of a page should have access to the page’s controls. However, since the function was in the original page object that was no longer active, it did not have access to any controls in the current page object that were to be rendered and sent to the browser.

Next, add some buttons and code to exercise the ObservableCollection:

<br />
<button id="ButtonAdd"  önclick="ButtonAdd_Click" text="Add"  runat="server" />
<button id="ButtonRemove"  önclick="ButtonRemove_Click" text="Remove"  runat="server" />
<button id="ButtonReset"  önclick="ButtonReset_Click" text="Reset"  runat="server" />
<button id="ButtonList"  önclick="ButtonList_Click" text="List"  runat="server" />
<br />   
<textbox id="TextBoxInt"  runat="server" width="51px" />
<br />  
<listbox id="ListBoxHistory"  runat="server" width="195px" height="255px">
// ---- Add Button --------------------------------------

protected void ButtonAdd_Click(object sender, EventArgs e)
    int Temp;
    if (int.TryParse(TextBoxInt.Text, out Temp) == true)

// ---- Remove Button --------------------------------------

protected void ButtonRemove_Click(object sender, EventArgs e)
    int Temp;
    if (int.TryParse(TextBoxInt.Text, out Temp) == true)

// ---- Button Reset -----------------------------------

protected void ButtonReset_Click(object sender, EventArgs e)

// ---- Button List --------------------------------------

protected void ButtonList_Click(object sender, EventArgs e)
    foreach (int i in MyInts)
        // a bit of tweaking to get the text to be indented
        ListItem LI = new ListItem("  " + i.ToString());
        LI.Text = Server.HtmlDecode(LI.Text);

Here's what it looks like after entering some numbers and clicking some buttons:

An interesting note: From the online help:

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

What does that mean to ASP.NET developers?

If you are going to share an ObservableCollection among different sessions, i.e. different threads, you’d better make it a static object and not use the Application state collection….right? WRONG!

The documentation is easily misread (there are a LOT of posts on this subject on a lot of web sites). It means static MEMBERS of the ObservableCollection class are thread-safe, not a static instance of the class itself. If the collection is to be shared among threads, you have to synchronize the access. Once again, I’d like to thank RichardD for his constant nagging and badgering me to get it right. Thanks pal.

I hope someone finds this useful.

Steve Wellens


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


About the Author

Steve Wellens
EndWell Software, Inc.
United States United States
I am an independent contractor/consultant working in the Twin Cities area in Minnesota. I work in .Net, Asp.Net, C#, C++, XML, SQL, Windows Forms, HTML, CSS, etc., etc., etc.

You may also be interested in...

Comments and Discussions

GeneralCool features Pin
Thanigainathan.S1-Jun-10 3:33
memberThanigainathan.S1-Jun-10 3:33 

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 | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 3 Jun 2010
Article Copyright 2010 by Steve Wellens
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid