title: Viewstate of ascx lost between postbacks
body: <p>In my ASP.NET application, I am loading an .ascx dynamically using LoadControl, using the following pattern:</p>
  
  <pre><code>var ctrl = LoadControl("/path/to/control.ascx");
  ((ControlType)ctrl).SomeProperty = someData;
  placeholder.Controls.Add(ctrl);
  </code></pre>
  
  <p>The control that I add saves the SomeProperty property value directly to ViewState, as follows:</p>
  
  <pre><code>public int? SomeProperty
  {
      get { return (int?)ViewState["SomeProperty"]; }
      set { ViewState["SomeProperty"] = value; }
  }
  </code></pre>
  
  <p>After that, the ascx control lives a life on its own and all is well until postback occurs. When the page posts back, suddenly the view state is empty! I suspect this happens because I manipulate the ViewState before I add the instantiated ascx to my page. Also, I can prevent the ViewState from getting lost by adding the following line in the <code>Page_Load()</code> method of my ascx control:</p>
  
  <pre><code>SomeProperty = SomeProperty;
  </code></pre>
  
  <p>I have to do the above for each and every property to ensure that the ViewState is preserved. Now, is there a prettier way of doing this? Manipulating the ViewState after the instantiated .ascx has been added to the page is not an option - I need the contents of the ViewState in the <code>Page_Init()</code> and <code>Page_Load()</code> methods, which are triggered the instant I add the .ascx to my page.</p>
  
  <p>Thanks.</p>