title: ActiveViewIndex is being set to '0'
body: <p>Alright... Searched online, followed about 100 answers, nothing is working so I am obviously not understanding something.</p>
  
  <p>I have a set of tabs that are dynamically created. When the user clicks one of the tabs, it should change to that tab and its content. However, every single time I try to do so, I get the following error message:</p>
  
  <blockquote>
    <p>ActiveViewIndex is being set to '0'.  It must be smaller than the current number of View controls '0'. For dynamically added views, make sure they are added before or in Page_PreInit event.</p>
  </blockquote>
  
  <p>Here's the ASP.NET code:</p>
  
  <pre><code>&lt;table width="90%" align="center"&gt;
      &lt;tr&gt;
      &lt;td&gt;
      &lt;asp:Panel ID="buttonPanel" runat="server" /&gt;
          &lt;asp:MultiView ID="ProfileView" runat="server"&gt;
          &lt;/asp:MultiView&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>Here's the C# code:</p>
  
  <pre><code>Button[] _tabs; //tabs for the profile
  View[] _views; //views inside of the MainView
  string[] _tabTitles = { "Main", "Website Stats", "About", "Other", 
                           "Wall", "Posts", "Topics", "Pictures" };
  
  protected void Page_PreInit(object sender, EventArgs e)
  {
      CreateTabs();
  }
  
  protected void Page_Load(object sender, EventArgs e)
  {
      for (int i = 0; i &lt; _tabs.Length; i++)
      {
          buttonPanel.Controls.Add(_tabs[i]);
          ProfileView.Controls.Add(_views[i]);
      }
      _tabs[0].CssClass = "Clicked";
      ProfileView.ActiveViewIndex = 0;
   }
  
  protected void CreateTabs()
  {
      _tabs = new Button[_tabTitles.Length];
      _views = new View[_tabTitles.Length];
  
      for (int i = 0; i &lt; _tabs.Length; i++)
      {
          _views[i] = new View();
          _views[i].ID = _tabTitles[i] + "View";
  
          _tabs[i] = new Button();
          _tabs[i].CssClass = "Initial";
          _tabs[i].BorderStyle = BorderStyle.None;
          _tabs[i].Text = _tabTitles[i];
          _tabs[i].Click += TabClick;
      }
  }
  
  protected void TabClick(object sender, EventArgs e)
  {
      for (int i = 0; i &lt; _tabs.Length; i++)
      {
          if ((sender as Button) == _tabs[i])
          {
              _tabs[i].CssClass = "Clicked";
              ProfileView.ActiveViewIndex = i;
          }
          else
              _tabs[i].CssClass = "Initial";
      }
  }
  </code></pre>
  
  <p>I did do debug and tried to see if TabClick was being called, but apparently it crashes the page before it even gets there. If I comment out:</p>
  
  <pre><code>ProfileView.ActiveViewIndex = 0;
  </code></pre>
  
  <p>I can click one tab, it switches over successfully, but then when clicking another tab it crashes with an error like:</p>
  
  <blockquote>
    <p>ActiveViewIndex is being set to '2'.  It must be smaller than the current number of View controls '0'. For dynamically added views, make sure they are added before or in Page_PreInit event.</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>