<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tutto .NET : StateManagedCollection</title><link>http://weblogs.asp.net/alessandro/archive/tags/StateManagedCollection/default.aspx</link><description>Tags: StateManagedCollection</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>A custom StateManagedCollection Vs a Generic List</title><link>http://weblogs.asp.net/alessandro/archive/2007/09/30/a-custom-statemanagedcollection-vs-a-generic-list.aspx</link><pubDate>Sun, 30 Sep 2007 23:35:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:4260981</guid><dc:creator>alessandro</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/alessandro/rsscomments.aspx?PostID=4260981</wfw:commentRss><comments>http://weblogs.asp.net/alessandro/archive/2007/09/30/a-custom-statemanagedcollection-vs-a-generic-list.aspx#comments</comments><description>&lt;P&gt;So, you have a collection class and you want it to maintain state. In the past versions of asp.net, we had to do this manually, managing state in our collection ourselves. It was not a difficult task but it required a bit of extra work. Now in asp.net 2.0 release, we dont need to do this ourselves anymore. Instead what we can do is take advantage of &lt;STRONG&gt;StateManagedCollection&lt;/STRONG&gt; class. This class asides from being a collection and providing us with the perfect base for our collections, it also manages viewstate automagically for us. In the past if your collection was inheriting CollectionBase, now you can instead inherit StateManagedCOllection. But first why do we need a collection ? I mean we could always just expose a property of type ArrayList of objects ? But then an ArrayList overgoes a lot of un-necessary boxing and unboxing, this is a useless overhead. But so what, asp.net 2.0 now supports generics and we can use the generic list ? The overhead is gone and we have a nice collection exposed, so why go through the trouble of creating a custom collection ? Especially when a generic List is giving us a nice easy, effortless collection Vs writing a strongly typed wrapper collection ourselves. extract from msdn docs : &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;It is to your advantage to use the type-specific implementation of the List class instead of using the ArrayList class or writing a strongly typed wrapper collection yourself. The reason is your implementation must do what the .NET Framework does for you already, and the common language runtime can share Microsoft intermediate language code and metadata, which your implementation cannot. &lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Now keeping this note in mind, consider that not always the List is what we want and we will need to resort to a custom collection we write ourselves. A good example is when we want the items in our list to participate in viewstate. I was unable to get this type of functionality. The list does not implement IStateManager, so it does not manage saving viewstate by itself. In order to save the items in our list, in viewstate, when using a generic list, we are going to have to make sure that our items contained in the list do not in turn contain complex types. If they do, then we need to make sure that those complex types are decorated with the serialize() attribute. That way their values can be serialized into viewstate. This is not usually in your control when you have a complex type like the Style class. Since this class has not been marked as serializable, you cannot just inherit it and mark your class as serializable and then voila. This will not work :-) &lt;BR&gt;Serialization cannot be added to a class after it has been compiled. Also not to mention that using this approach of marking our class as serializable will result in pretty bad performance, especially in bloated data and overhead of serialization which takes quite a long time.&lt;/P&gt;
&lt;P&gt;So for complex types like these, since we cant serialize them, we also cant have them participate in our controls viewstate. Remember complex types must manage their own viewstate by implementing IStateManager. If however the properties types that we are exposing in our Item are basic types and/or complex types that we have control over, this is possible. However it looks pretty dirty imho. The reason I say dirty is becuase we can not control at the property level how items are adding to viewstate, meaning that we will be storing all items in viewstate whether their values have changed or not. This is not very convenient for me and results in a beautiful bloated viewstate which i prefer to avoid. &lt;/P&gt;
&lt;P&gt;These advantages are however there when writing your own custom collections class and its not all that hard. Here in this post, i want to demonstrate how to create your own custom collection while allowing it to participate in viewstate for FREE*, that is with very little effort coming from your part by using a new class in the .net framework --&amp;gt;&amp;gt; StateManagedCollection. In addition creating our custom collection also allows us to nest collections within collections, within collections, which i have found very useful. This, i am not demonstrating here but once you know how to create a custom collection, nesting collections is very simple and a no brainer so i dont want to waste time here with that :-) &lt;/P&gt;
&lt;P&gt;I am not going to walk you through the code, because its very simple, basic code example. I have left comments inline, some useless, some are good notes and understanding them is essential, so make sure you dont miss them. &lt;/P&gt;
&lt;P&gt;Note that since we will be using a CollectionEditor, so that you can get a beautiful dialog for adding items to your collection at designtime in visual studio 2005, then you need to reference one managed dll : System.Design. this dll is not added into a web application project by default, so you have to reference it yourself. &lt;/P&gt;
&lt;P&gt;The output of this custom control are the 4 items we added to our collection, each item represented with a color. There is no purpose for this control other than to demonstrate collections, so functionality wise its pretty dumb :D I tried to keep it as simple as possible. Posting back by clicking the button in the webform should cause the control to load these values from viewstate, and its perfect to see how this is all working nicely and here is the rendered output : &lt;/P&gt;
&lt;DIV id=MyControl1&gt;&lt;SPAN&gt;This collection was loaded from viewstate ? True/False&lt;/SPAN&gt; &lt;BR&gt;&lt;SPAN style="BACKGROUND-COLOR: blue"&gt;blue&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: green"&gt;green&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: yellow"&gt;yellow&lt;/SPAN&gt;&lt;SPAN style="BACKGROUND-COLOR: beige"&gt;beige&lt;/SPAN&gt;&lt;INPUT disabled type=submit value="Lets postback, c'mon click me, please please click me :P" name=ctl06&gt; &lt;/DIV&gt;
&lt;DIV class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:51bcccb4-3409-4ede-9058-79db143a618e contentEditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;PRE style="BACKGROUND-COLOR: white"&gt;&lt;DIV&gt;&lt;!--  Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/  --&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.Web.UI;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.Web.UI.WebControls;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.ComponentModel;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;namespace&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; CustomControl
{
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; Summary description for MyControlItem
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;    &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlItem : IStateManager
   {
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlItem()
       {
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;string&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyProperty
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
           {
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; o &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; ViewState[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;MyPropertyValue&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;];
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (o &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;==&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;?&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;string&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.Empty : o.ToString();
           }
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;set&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; { ViewState[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;MyPropertyValue&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;] &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; value; }
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;/*&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
        Only need to call TrackViewState on the complex type (style) itself. The style class
        or rather the complex type is always responsible for maintaining its own viewstate.
        Our job is to simply call LoadViewState, SaveViewState and TrackViewState methods exposed
        by our complex types. To see that, look at the LoadViewState, SaveViewState and TrackViewState methods
        in this class. Since this is a complex type too it implements IStateManager interface. Our Style
        class also implements IStateManager like we are doing here..
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;*/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Style myControlStyleValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;;
       [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
       PersistenceMode(PersistenceMode.InnerProperty), NotifyParentProperty(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;true&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)]
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Style MyControlStyle
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
           {
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (myControlStyleValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;==&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
               {
                   myControlStyleValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Style();
                   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (isTrackingViewStateValue)
                   {
                       ((IStateManager)(myControlStyleValue)).TrackViewState();
                   }
               }
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; myControlStyleValue;
           }
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;/*&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
        Store all our properties in the statebag exposed by this property. All properties that are not
        complex types can be stored by our custom implementation of the statebag.
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;*/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; StateBag viewStateValue;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; StateBag ViewState
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
           {
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (viewStateValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;==&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
               {
                   viewStateValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; StateBag(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;false&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;);
                   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (isTrackingViewStateValue)
                   {
                       ((IStateManager)viewStateValue).TrackViewState();
                   }
               }
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; viewStateValue;
           }
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;bool&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; isTrackingViewStateValue;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;bool&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; IStateManager.IsTrackingViewState
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
           {
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; isTrackingViewStateValue;
           }
       }

       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; IStateManager.SaveViewState()
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.SaveViewState();
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; IStateManager.LoadViewState(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; savedState)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.LoadViewState(savedState);
       }

       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;internal&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; LoadViewState(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; savedState)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[] states &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[])savedState;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (states &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
           {
               ((IStateManager)ViewState).LoadViewState(states[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;]);
               ((IStateManager)MyControlStyle).LoadViewState(states[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;]);
           }
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;internal&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; SaveViewState()
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[] states &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;];
           states[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;] &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (viewStateValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;?&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; ((IStateManager)viewStateValue).SaveViewState() : &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;;
           states[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;] &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (myControlStyleValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;?&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; ((IStateManager)myControlStyleValue).SaveViewState() : &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; states;
       }

       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; IStateManager.TrackViewState()
       {
           isTrackingViewStateValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;true&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (viewStateValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
               ((IStateManager)viewStateValue).TrackViewState();
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (myControlStyleValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
               ((IStateManager)myControlStyleValue).TrackViewState();
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;/*&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
        This is important. Without this call, state wont be maintained.
        This method is being called by our collections SetDirtyObject method, which in turn
        is called internally by StateManagedCollection's SaveViewState method, Add, and Insert methods.
        The SetDirtyObject allows you to save viewstate information at the element level.
        In this method call of SetDirty we call set dirty individually
        only on the properties that manage state themselves.
        So its the only way that this class will know its items have changed and it needs to save it to viewstate.
        Forget to do this and viewstate wont be maintained.
        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;*/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;internal&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; SetDirty()
       {
           viewStateValue.SetDirty(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;true&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;);
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (myControlStyleValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
               myControlStyleValue.SetDirty();
       }
   }
}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4109c867-1e17-4d14-8c67-9ee7876ca7c1 contentEditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; WIDTH: 888px; PADDING-TOP: 0px"&gt;&lt;PRE style="BACKGROUND-COLOR: white"&gt;&lt;DIV&gt;&lt;!--  Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/  --&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.Web.UI;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.Web.UI.WebControls;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.ComponentModel;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.Collections;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;namespace&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; CustomControl
{
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; Summary description for MyControlCollection
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;    &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlCollection : StateManagedCollection
   {
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlCollection()
       {
       }

       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;static&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;readonly&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Type[] knownTypes;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;static&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlCollection()
       {
           MyControlCollection.knownTypes &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Type[] { &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;typeof&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;(MyControlItem) };
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;/*&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;Note how we cast to IList, this is because our base class implements IList
        and adds our elements to an IList.&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;*/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlItem &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; index]
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
           {
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (MyControlItem)((IList)&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)[index];
           }
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;set&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
           {
               ((IList)&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)[index] &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; value;
           }
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; Add an item to the collection&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Add(MyControlItem value)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; ((IList)&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;).Add(value);
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; Get the index of the value, passed in as parameter&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; IndexOf(MyControlItem value)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; ((IList)&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;).IndexOf(value);
       }

       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;Insert an item in the collection by index&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Insert(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; index, MyControlItem value)
       {
           ((IList)&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;).Insert(index, value);
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;/*&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;Copies the elements of the StateManagedCollection collection to an array,
         starting at a particular array index&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;*/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; CopyTo(MyControlItem[] toolBarSetArray, &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; index)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;base&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.CopyTo(toolBarSetArray, index);
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;/*&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; Overide and create an instance of the class that implements IStateManager.
          This class is ofcourse our item that we are adding to our collection. In this case
          the class is MyControlItem. its used internally by the base class (StateManagedCollection) when
          saving the item in viewstate. If by chance your class type can vary, then you can check
          the index and create a new class based on the index value. the index will vary based on
          the order of the types you specified in GetKnownTypes. For a good concreate example look up
          StateManagedCollection class on msdn. For this example, we have only one type, and the index
          is always zero, so there is no need to check the index before returning a new instance of our type&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;*/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;protected&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; CreateKnownType(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; index)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlItem();
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;/*&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;This method should return an array of IStateManager types that
         the StateManagedCollection collection can contain.
         For a good concreate example look up StateManagedCollection class on msdn.&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;*/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;protected&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Type[] GetKnownTypes()
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlCollection.knownTypes;
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; Remove an item from the collection&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Remove(MyControlItem value)
       {
           ((IList)&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;).Remove(value);
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;Remove an item by index from the collection&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; RemoveAt(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;int&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; index)
       {
           ((IList)&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;).RemoveAt(index);
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;Does our collection contain the following item in the collection ?&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;bool&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Contains(MyControlItem value)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; If value is not of type MyControlItem, this will return false.&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;            &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; ((IList)&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;).Contains(value);
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; Validate and allow types that your collection will contain&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;protected&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; OnValidate(Object value)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (value.GetType() &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;typeof&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;(MyControlItem))
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;throw&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; ArgumentException(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;value must be of type MyControlItem.&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;value&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;);
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;/*&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;instructs an object contained by the collection
         to record its entire state to view state,
         rather than recording only change information&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;*/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;protected&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; SetDirtyObject(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; o)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (o &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;is&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlItem)
           {
               ((MyControlItem)o).SetDirty();
           }
       }
   }
}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f0a9aa6c-09b9-4f83-beab-23a31e7157c3 contentEditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;PRE style="BACKGROUND-COLOR: white"&gt;&lt;DIV&gt;&lt;!--  Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/  --&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.Web.UI;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.Web.UI.WebControls;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;using&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.ComponentModel;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;namespace&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; CustomControl
{
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;&amp;lt;summary&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; Summary description for MyControl
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;///&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;&amp;lt;/summary&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,128,128)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;    &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;class&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControl : WebControl
   {
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControl()
           : &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;base&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; TODO: Add constructor logic here
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;        }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;bool&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; isLoadedFromViewState &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;true&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;;
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlCollection myControlCollectionValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;;
       [Category(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;Behavior&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;), Description(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;A collection of ToolBarItem's &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;),
       DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
       Editor(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;typeof&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;(System.ComponentModel.Design.CollectionEditor), &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;typeof&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;(System.Drawing.Design.UITypeEditor)),
       PersistenceMode(PersistenceMode.InnerProperty)]
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlCollection MyControlCollection
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;get&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
           {
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (myControlCollectionValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;==&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
                   myControlCollectionValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlCollection();
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (IsTrackingViewState)
               {
                   ((IStateManager)(myControlCollectionValue)).TrackViewState();
               }
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; myControlCollectionValue;
           }
       }

       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;protected&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; LoadViewState(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; state)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (state &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
           {
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[] states &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[])state;
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;base&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.LoadViewState(states[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;]);
               ((IStateManager)MyControlCollection).LoadViewState(states[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;]);
           }
       }

       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;protected&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; SaveViewState()
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[] states &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;object&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;2&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;];

           states[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;] &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;base&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.SaveViewState();
           states[&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;] &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (myControlCollectionValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;) &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;?&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; ((IStateManager)myControlCollectionValue).SaveViewState() : &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; states;
       }

       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;protected&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; TrackViewState()
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;base&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.TrackViewState();
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (myControlCollectionValue &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;!=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;null&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
               ((IStateManager)myControlCollectionValue).TrackViewState();
       }

       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;protected&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; OnLoad(EventArgs e)
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;base&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.OnLoad(e);
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;/*&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
            just create some defaults and add them to our collection. Since we are adding
            new items to our collection at runtime, viewstate should kick in during postback
            and rebuild the values from there. So only set some defaults if our collection is empty. Meaning
            this is the first time we are adding items to our collection :-)
            This is perfect to test if viewstate on my collection is working.
            &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;*/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.MyControlCollection.Count &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;1&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
           {
               isLoadedFromViewState &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;false&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;;
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;string&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[] defaults &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;string&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;[] { &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;blue&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;green&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;yellow&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;, &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;beige&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; };
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;foreach&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;string&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; d &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;in&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; defaults)
               {
                   MyControlItem mci &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; MyControlItem();
                   mci.MyProperty &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; d;
                   mci.MyControlStyle.BackColor &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; System.Drawing.Color.FromName(d);
                   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.MyControlCollection.Add(mci);
               }
           }
       }
       &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;protected&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; CreateChildControls()
       {
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;base&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.CreateChildControls();
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.MyControlCollection.Count &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;0&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;)
           {
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.Controls.Add(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; LiteralControl(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;string&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.Format(&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;&amp;lt;h1&amp;gt;This collection was loaded from viewstate ? {0}&amp;lt;/h1&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;, isLoadedFromViewState.ToString())));
               &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;foreach&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; (MyControlItem mci &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;in&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.MyControlCollection)
               {
                   Label l &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Label();
                   l.Text &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; mci.MyProperty;
                   l.ControlStyle.CopyFrom(mci.MyControlStyle);
                   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.Controls.Add(l);
               }
           }
           Button b &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; Button();
           b.Text &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;Lets postback, c'mon click me, please please click me :P&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;;
           &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;this&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;.Controls.Add(b);&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;//&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt; dummy postback to check viewstate&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;        }
   }
}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;DIV class=wlWriterSmartContent id=57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:12643445-c53a-4282-b1c8-40d92fca8d0d contentEditable=false style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; FLOAT: none; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;&lt;PRE style="BACKGROUND-COLOR: white"&gt;&lt;DIV&gt;&lt;!--  Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/  --&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(255,255,0)"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;@ Page Language&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;C#&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(255,255,0)"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(255,255,0)"&gt;&amp;lt;%&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;@ Register TagPrefix&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;ctl&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt; Namespace&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;CustomControl&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt;"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(245,245,245)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0); BACKGROUND-COLOR: rgb(255,255,0)"&gt;%&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;!&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(255,0,255)"&gt;DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;

&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;html &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(255,0,0)"&gt;xmlns&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;="http://www.w3.org/1999/xhtml"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(255,0,0)"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;head &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(255,0,0)"&gt;runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;Untitled Page&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;title&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;head&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;form &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(255,0,0)"&gt;id&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;="form1"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(255,0,0)"&gt; runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;ctl:MyControl &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(255,0,0)"&gt;ID&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;="MyControl1"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(255,0,0)"&gt; runat&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;="server"&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;ctl:MyControl&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;div&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
   &lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;form&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;body&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,0)"&gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(128,0,0)"&gt;html&lt;/SPAN&gt;&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=4260981" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/alessandro/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/alessandro/archive/tags/StateManagedCollection/default.aspx">StateManagedCollection</category><category domain="http://weblogs.asp.net/alessandro/archive/tags/Generic+List/default.aspx">Generic List</category></item></channel></rss>