I thought I knew ASP.NET. I started ASP.NET programming in 2001, and I started it with customer/server control development and was cranking them out with no problem. So theoretically I should not have struggled for six hours with a CompositeControl not being able to save nested controls' state between postbacks.
All it came down to is calling EnsureChildControls() from the OnInit().
[
ToolboxData("<{0}:BogusCustomControl runat=server></{0}:BogusCustomControl>")]
public class BogusCustomControl : CompositeControl
{
Button btn = new Button();
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.EnsureChildControls(); // << This is it! This makes ViewState work for a CompositeControl
}
protected override void OnLoad(EventArgs e)
{
if (!this.Page.IsPostBack)
{
// Set value once to test whether it's preserved between postbacks
this.btn.Text = DateTime.Now.ToLongTimeString();
}
base.OnLoad(e);
}
protected override void CreateChildControls()
{
this.btn.ID = "whatever";
this.Controls.Add(this.btn);
base.CreateChildControls();
}
}