ASP.NET Bug, Multi View control do not save ViewState, of dynamically added controls
Couple of days back me and my colleague, we discovered an issue with the ASP.NET Multi View Control.
We were surprised to see that it do not add ViewState, of the dynamically added controls, of the Inactive Tabs.
ASP.NET 2.0
<asp:MultiView ID="MultiView1" runat="server" EnableViewState="true">
</asp:MultiView>
C# Code Behind
if (!IsPostBack)
{
int index = 1;
foreach (View v in MultiView1.Views)
{
TextBox t = new TextBox();
t.ID = index.ToString();
t.Text = "This text will not be assigned, to any Inactive Tabs, unless you put a breakpoint on this line and watch the value of this line explicitly";
v.Controls.Add(t);
index = index + 1;
}
}
Surprisingly, you will notice only the Textbox.Text of the Active Tab will have value,
however if you go to any other Tab of the MultiViewControl, you will notice that the TextBoxes are empty.
After investigating further we realized that the Viewstates of the dynamically added controls are not saved (for any of the inactive tabs).
It became more interesting, when we started to debug, by putting a breakpoint to watch TextBox.Text, surprisingly every TextBox gets populated with desired Text ( for all tabs including the inactive tab, only when you explicitly watch ). Also it saves all ViewState correctly.
Not sure whether its a bug, the ASP.NET team may have wanted this behavior to enhance performance of the Multi View control,
but if that is the case, why does it populate the TextBox.Text and also saves into Viewstate, when we try to debug !!!
Do not believe me? Try it by yourself !!