Markup looks like this:
<form id="form1" runat="server">
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Do Something" />
</form>
Code behind looks like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
txt1.Visible = false;
Form.Controls.AddAt(0, new TextBox() { ID = "blah", Text = "blah", EnableViewState = true });
}
The problem is that when I click on the button, txt1 becomes visible again, even though I had set it's visibility to hidden. Viewstate should have retained it's visibility to hidden but for some reason does not.
Important: This only happens when I dynamically add a control using Form.Controls.AddAt. Doing so seems to mess up the viewstate for all controls after the dynamically added control.
Any ideas why? Or how to use AddAt without messing up the viewstate for all subsequent controls on the page?