title: UserControl Within Template of another UserControl Loses values on postback
body: <p>This may be the really wrong way of doing this and so if it is please let me know!! I have a user control called ucDropDownList.ascx. It basically is a drop down with a button besides it. When the button is clicked a jquery dialog appears. The dialog is the problem. As this user control is used in a number of places i wanted (within the principles of OOP) to allow us to load in a different form depending on which drop down it was...So i went for Template User Control. The code behind for this is as follows:</p>
  
  <pre><code>public partial class ucDropDownList : System.Web.UI.UserControl
  
   {
  
     private ITemplate m_RecordForm = null;
  
     [TemplateContainer(typeof(cPopUpContainer))]
     [PersistenceMode(PersistenceMode.InnerProperty)]
     public ITemplate RecordForm
     {
       get { return m_RecordForm; }
       set { m_RecordForm = value; }
     }
  
     protected void Page_Load(object sender, EventArgs e)
     {
  
     }
  
     void Page_Init()
     {
       // Load in the Template control (we use this for making the different types of form!
       if (RecordForm != null)
       {
         cPopUpContainer container = new cPopUpContainer();
         m_RecordForm.InstantiateIn(container);
         phNewRecordForm.Controls.Add(container);
       }
     }
  
     public event EventHandler SaveClicked;
     protected void SaveButton_Clicked(object sender, EventArgs e)
     {
       if (SaveClicked != null)
         SaveClicked(this, e);
     }
   }
  
   public class cPopUpContainer: Control, INamingContainer
   {
     internal cPopUpContainer()
     {
     }
   }
  </code></pre>
  
  <p>Then i (will) have several user controls that are simple forms for each of the types that i will have these user controls for...(example given is a status table in the database). There is nothing exciting there, just that all of the controls have viewState Enabled. (pretty much as standard as it comes). </p>
  
  <p>The page that i show these on has some markup like so...</p>
  
  <pre><code>&lt;uc3:ucDropDownList ID="comboStatus" runat="server" OnSaveClicked="comboStatus_Saved"&gt;
                      &lt;RecordForm&gt;
                          &lt;uc4:ucEdStatus ID="ucEdStatus1" runat="server" ViewStateMode="Enabled" /&gt;
                      &lt;/RecordForm&gt;
                  &lt;/uc3:ucDropDownList&gt;
  </code></pre>
  
  <p>and a behind the scenes like so...</p>
  
  <pre><code>  protected void Page_Init(object sender, EventArgs e)
    {
       if (!IsPostBack)
      {
        ucEdStatus statForm = comboStatus.Controls[3].Controls[0].FindControl("ucEdStatus1") as ucEdStatus;
        statForm.InitializeControl(false);
      }
     } 
  
    protected void Page_Load(object sender, EventArgs e)
    {
  
    }
  
    protected void comboStatus_Saved(object sender, EventArgs e)
    {
      ucEdStatus statForm = comboStatus.Controls[3].Controls[0].FindControl("ucEdStatus1") as ucEdStatus;
      statForm.SaveRecord(eSaveUCEdType.Insert);
    }
  </code></pre>
  
  <p>When the SaveRecord runs(this method is a simple insert into the database which is based on the values of the various controls on the ucEdStatus1 control) the controls on the ucEdStatus are all the default as set in the markup and non have maintained what the user put in (therefore the save just saves a wrong bit of data). </p>
  
  <p>I am open to suggestions and criticism. If you need more information i can give it to you, i just need help in understanding why this is happening. </p>
  
  <p>EDIT
  Just in case you guys were wondering this is the class for the ucEdStatus control. It may answer why i am still scratching my head or may show how stupid i am. </p>
  
  <pre><code>long m_RecordID
  {
    get { return (ViewState["recordid"] != null ? Convert.ToInt64(ViewState["recordid"]) : -1); }
    set { ViewState["recordid"] = value; }
  }
  
  bool m_isSupport
  {
    get { return (ViewState["isSupport"] != null ? Convert.ToBoolean(ViewState["isSupport"]) : false); }
    set { ViewState["isSupport"] = value; }
  }
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  
  //--------------------------------------------------------------------------------
  public void InitializeControl(bool aIsSupport)
  {
    //Setup the issupport variable
    m_isSupport = aIsSupport;
  
    //Add the importance to the mixup...
    Array importanceTexts = System.Enum.GetNames(typeof(eStatusImportance));
  
    for (int i = 0; i &lt;= importanceTexts.Length - 1; i++)
    {
      ListItem item = new ListItem(importanceTexts.GetValue(i).ToString(), i.ToString());
      comboImportance.Items.Add(item);
    }
  }
  
  //--------------------------------------------------------------------------------
  public long SaveRecord(eSaveUCEdType aSaveType)
  {
    using (cDBConnection con = new cDBConnection(this.Page, true))
    {
      cStatus stat = new cStatus(con.Con, m_RecordID);
      if (m_RecordID == -1)
        stat.Id = cGlobalDB.NewKey();
  
      stat.Importance = Convert.ToInt32(comboImportance.SelectedValue);
      stat.Name = txtName.Text;
      stat.Issupport = m_isSupport;
      stat.Color = edColor.Color;
      stat.SaveChanges((aSaveType == eSaveUCEdType.Insert ? eUpdateType.insert : eUpdateType.update));
      return (long)stat.Id;
    }
  }
  </code></pre>