title: Dynamic User Controls get and maintain values after postbacks
body: <p>I have a page that I have built that is dynamically generating user controls.  For example, the user control has two fields that can be edited:  SomeId and SomeData.  I want to be able to set these values based on a <code>UserControlObject</code>.  When I read the control afterwards, I want to be able to return a <code>UserControlObject</code> from the control and set the database values to that.  I am having some trouble getting the values to stay current.</p>
  
  <pre><code>//Page
  public class SomePage
  {
      private List&lt;MyUserControl&gt; _MyUserControls;
  
      private void InitControls()
      {
        var controlsToBuild = SomeDatabaseCallToGetControlObjects();
  
            foreach(MyUserControl control in controlsToBuild)
            {
          MyUserControl control = this.Page.LoadControl("MyControl.ascx") as MyUserControl;
  
            UserControlPlaceHolder.Controls.Add(myControl);
            _MyUserControls.Add(myControl);
        }
      }
  
      protected void Page_Init(object sender, EventArgs e)
      {
         InitControls();
      }
  
      protected void SaveButton_Click(object sender, EventArgs e)
      {
        if (this.IsValid)
        {
          List&lt;MyUserObject&gt;listObj = new List&lt;MyUserObject&gt;();
          foreach(MyUserControl control in _MyUserControls)
          {
             MyUserObject obj = control.GetObjectFromControl();
             listObjs.Add(obj);
          }
          SaveToDatabase(listObjs);
          //Clear placeholder in case number of controls has changed based on the save
          UserControlPlaceHolder.Clear();
          InitControls();
          GetObjectFromDatabase();
        }
      }
  
  }
  
  //Object to put in control
  public class UserControlObject
  {
      public string DataForUser { get;set;}
      public double MoreDataForUser { get;set;}
      public int HiddenIdFromUser { get; set;}
  }
  
  
  //User control
  public class MyUserControl : UserControl
  {
  
    public MyUserControlObject GetObjectFromControl()
    {
       UserControlObject obj = new UserControlObject();
       obj.DataForUser = textBoxOnTheControl.Text;
       obj.MoreDataForUser = anotherBoxOnTheControl.Text;
       obj.HiddenIdFromUser = hiddenField.Value;
  
       return obj;
    }
  
    public void SetUserControlObject(UserControlObject obj)
    {
       textBoxOnTheControl.Text = obj.DataForUser;
       anotherBoxOnTheControl.Text = obj.MoreDataForUser;
       hiddenField.Value = obj.HiddenIdFromUser;
    }
  }
  </code></pre>
  
  <p>Basically, I want to load up each <code>MyUserControl</code> with values from a database.  A user should be able to change most (but not all) of those fields.  A user shouldn't be able to change the ID, but I need it to update the record in the database.</p>
  
  <p>I am having some trouble getting the information to save and persist correctly, because I am re-creating a collection of <code>MyUserControl</code>.  It seems to work fine on the inital postback, but after subsequent postbacks, I still get the old value for one of the fields.  How can I ensure that:</p>
  
  <p>1) The information is set to the user control(s) correctly.  Do I need to put a public property referencing viewstate on <em>every</em> property of the UserControl?</p>
  
  <p>2) After clicking a save event, after postback, rebuilding the controls won't cause me to retain any "old" values from the initial load?  How should I handle the viewstate here?  I want the data to reflect whatever the user has changed.</p>