title: How to add View to Multiview dynamically in asp.net
body: <p>I have placed MultiView in UpdatePanel and added Views dynamically, each has GridView control. I'm able to display only the View which is set at first time. I have added static button for &quot;Next&quot; &amp; &quot;Back&quot;. An error <em>&quot;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.Parameter name: value&quot;</em> occurs, when I click any button including these two.</p>
  <p>Code-behind:</p>
  <pre><code>protected void MultiView1_Load(object sender, EventArgs e)
      {
          try
          {
              if (DropDownList4.SelectedIndex &gt; 0)
              {
                  int i = 1;
                  SqlCommand cmd = new SqlCommand(&quot;select DISTINCT(schedule) from tender where project = '&quot; + DropDownList1.SelectedItem.ToString() + &quot;'&quot;, agr);
                  SqlDataReader dr = cmd.ExecuteReader();
                  while (dr.Read())
                  {
                      string schedule = dr[0].ToString().Trim();
                      GridView Gridview1 = new GridView();
                      Gridview1.ID = &quot;Gridview&quot; + i.ToString();
                      Gridview1.RowDataBound +=new GridViewRowEventHandler(Gridview1_RowDataBound);
                      Gridview1.EnableViewState = true;
                      Gridview1.HeaderStyle.BackColor = System.Drawing.Color.Silver;
                      Gridview1.HeaderStyle.ForeColor = System.Drawing.Color.White;
                      Gridview1.HeaderStyle.Font.Bold = true;
                      Gridview1.ForeColor = System.Drawing.Color.Gray;
                      Gridview1.AlternatingRowStyle.BackColor = System.Drawing.Color.White;
                      Gridview1.GridLines = GridLines.None;
                      Gridview1.RowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml(&quot;#EFF3FB&quot;);
                      Gridview1.HorizontalAlign = HorizontalAlign.Center;
                      Gridview1.Width = 900;
                      DataTable dt = new DataTable();
                      dt.Columns.Add(&quot;SN&quot;, typeof(string));
                      dt.Columns.Add(&quot;SCHEDULE&quot;, typeof(string));
                      dt.Columns.Add(&quot;MATERIAL&quot;, typeof(string));                    
                      dt.Columns.Add(&quot;UNIT&quot;, typeof(string));
                      dt.Columns.Add(&quot;PREVIOUS BOQ QTY&quot;, typeof(string));
                      dt.Columns.Add(&quot;CURRENT QTY&quot;, typeof(string));
                      int j = 1;
                      SqlCommand cmd1 = new SqlCommand(&quot;select material from tender where project = '&quot; + DropDownList1.SelectedItem.ToString() + &quot;' AND schedule = '&quot; + schedule + &quot;' order by schedulesubsn &quot;, agr);
                      SqlDataReader dr1 = cmd1.ExecuteReader();
                      while (dr1.Read())
                      {
                          DataRow row = dt.NewRow();
                          string mat = dr1[0].ToString().Trim();
                          row[&quot;SN&quot;] = j;
                          row[&quot;SCHEDULE&quot;] = schedule;
                          row[&quot;MATERIAL&quot;] = mat;
                          row[&quot;UNIT&quot;] = Calfunc.getunit(mat);
                          row[&quot;PREVIOUS BOQ QTY&quot;] = Calfunc.GetLocationBOQ(DropDownList1.SelectedItem.ToString(), DropDownList2.SelectedItem.ToString(), DropDownList3.SelectedItem.ToString(), DropDownList4.SelectedItem.ToString(), schedule, mat);
                          dt.Rows.Add(row);
                          j++;
                      }
                      dr1.Dispose();                 
                      View view = new View();
                      view.ID = schedule;                    
                      view.Controls.Add(Gridview1);
                      MultiView1.Views.Add(view);
                      Gridview1.DataSource = dt;
                      Gridview1.DataBind();
                      i++;                    
                  }
                  dr.Dispose();
                  MultiView1.ActiveViewIndex = i-3;
                  
              }
          }
          catch (Exception ex)
          { 
          }
      }
      protected void Button3_Click(object sender, EventArgs e)
      {
          if (MultiView1.ActiveViewIndex &gt; 0)
          {
              MultiView1.ActiveViewIndex = MultiView1.ActiveViewIndex - 1;                
          }
      }
      protected void Button4_Click(object sender, EventArgs e)
      {
          if (MultiView1.Views.Count-1 &gt; MultiView1.ActiveViewIndex)
          {
              TextBox2.Text = (MultiView1.ActiveViewIndex + 1).ToString();
          }
      }
      protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
              TextBox TextBox101 = new TextBox();
              TextBox101.ID = &quot;TextBox_C_QTY&quot;;
              TextBox101.Width = 70;
              TextBox101.Text = (e.Row.DataItem as DataRowView).Row[&quot;CURRENT QTY&quot;].ToString();
              e.Row.Cells[5].Controls.Add(TextBox101);
          }
      }
  </code></pre>
  <p>aspx:</p>
  <pre><code>&lt;asp:UpdatePanel ID=&quot;UpdatePanel4&quot; runat=&quot;server&quot;&gt;
      &lt;ContentTemplate&gt; 
      &lt;div  style=&quot;overflow-x:auto;&quot;&gt;
          &lt;asp:MultiView ID=&quot;MultiView1&quot; runat=&quot;server&quot; onload=&quot;MultiView1_Load&quot;&gt;
             
          &lt;/asp:MultiView&gt;        
      &lt;/div&gt;
                 &lt;/ContentTemplate&gt;
      &lt;/asp:UpdatePanel&gt;
  </code></pre>
  <p>I've searched for the solution but didn't find any satisfactory or workable (for me).</p>