Click here to Skip to main content
6,821,293 members and growing! (19,449 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate License: The Code Project Open License (CPOL)

Generic State Collection

By Velio Ivanov

Generic implementation of IStateManager for ASP.NET state collections.
C# (C#1.0, C#2.0, C#3.0).NET2.0, .NET3.0, .NET3.5, ASP.NET, Dev
Posted:27 Jan 2008
Updated:22 Mar 2008
Views:10,575
Bookmarked:11 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 1.66 Rating: 2.75 out of 5
2 votes, 50.0%
1

2

3
1 vote, 25.0%
4
1 vote, 25.0%
5
Download StateCollection.zip - 925 B

Introduction

While building custom ASP.NET controls I often need to implement IStateManager for collections of objects in order to persist them in ViewState.
The common way was to create collection by inherit List<T> and implementing IStateManager.
In most of the cases the code was same, I just have to change the generic type of the list.
That's why it was a perfect candidate for generic implementation. Implementation which made

The Generic State Collection gave me the ability to use any kind of state collections without a line of additional code.

The Code

StateCollection inherits List<T> class and implements IStateManager. Please, note I'm requiring the generic type of StateCollection to be one that implements IStateManager by itself.

Here is the source code of StateCollection class:

/// <summary>
/// 
/// </summary>
/// <typeparam name="T"></typeparam>
public class StateCollection<T> : List<T>, IStateManager where T : IStateManager, new(){

#region Fields  /////////////////////////////////////////////////////////////////

bool _tracking;

#endregion

#region Properties  /////////////////////////////////////////////////////////////

/// <summary>
/// When implemented by a class, gets a value indicating whether a server control is tracking its view state changes.
/// </summary>
/// <value></value>
/// <returns>true if a server control is tracking its view state changes; otherwise, false.</returns>
public bool IsTrackingViewState {
    get { return _tracking; }
}
#endregion

#region Methods /////////////////////////////////////////////////////////////////

/// <summary>
/// Loads the state of the view.
/// </summary>
/// <param name="savedState">State of the saved.</param>
public void LoadViewState(object savedState) {

    object[] state = savedState as object[];
    if (state != null) {
        T item;
        bool exists;
        for (int i = 0; i < state.Length; i++) {
            item = (exists =( i < this.Count)) ? this[i] : new T();
            item.LoadViewState(state[i]);
            if (this.IsTrackingViewState)
                item.TrackViewState();
            if(!exists) Add(item);
        }
    }
}

/// <summary>
/// When implemented by a class, saves the changes to a server control's view state to an <see cref="T:System.Object"/>.
/// </summary>
/// <returns>
/// The <see cref="T:System.Object"/> that contains the view state changes.
/// </returns>
public object SaveViewState() {

    if (this.Count > 0) {
        int count = this.Count;
        object[] state = new object[count];
        for (int i = 0; i < count; i++) {
            state[i] = this[i].SaveViewState();
        }
        return state;
    }
    else
        return null;
}

/// <summary>
/// When implemented by a class, instructs the server control to track changes to its view state.
/// </summary>
public void TrackViewState() {
    _tracking = true;
}
#endregion
}

Points of Interest

As example of how to use StateCollection you can refer to GoogleMap Control source code.

History

  • 22.Mar.2008 - Initial release

License

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

About the Author

Velio Ivanov


Member

Occupation: Web Developer
Location: Bulgaria Bulgaria

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralWhy ? PinmemberXmen W.K.2:49 26 Nov '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 22 Mar 2008
Editor:
Copyright 2008 by Velio Ivanov
Everything else Copyright © CodeProject, 1999-2010
Web11 | Advertise on the Code Project