title: Control 'ctl02' of type 'Button' must be placed inside a form tag with runat=server
body: <p>I get that error when trying to generate a number of buttons programmatically. I have no ctl02.. Why do i get that mistake?</p>
  
  <pre><code>  Button pgs = new Button();//Create New Topic
                          pgs.Width = 20;                        
                          pgs.Command += obtainTopicsPerPage_Click;
                          pgs.CommandName = tPage.ToString();
                          pgs.Text = tPage.ToString();
  
                          btns.Add(tPage.ToString());
                          buttons.Add(pgs);
  </code></pre>
  
  <p>I create a few buttons and loop through the list (buttons). Then i get that mistake :(. ... why?</p>
  
  <p>Full design:</p>
  
  <pre><code>int maximumTopicPages;
  int tPage;
  int questionNumber=1;
  Dictionary&lt;string, List&lt;DisplayAllQuestionsTable&gt;&gt; tPages;
  List&lt;Button&gt; buttons = new List&lt;Button&gt;();
  protected void Answer_Click(object sender, EventArgs e)
  {
      ViewState["SeekPressed"] = "pressed";
      tPages = new Dictionary&lt;string, List&lt;DisplayAllQuestionsTable&gt;&gt;();
      string subTopic = SubTopicDropDownList.SelectedItem.Value;
      List&lt;DisplayAllQuestionsTable&gt; threadsByTopic = new List&lt;DisplayAllQuestionsTable&gt;();
      List&lt;string&gt; btns = new List&lt;string&gt;();
  
      foreach (var topicKeys in postsByTopic)
      {
  
             if (topicKeys.Key == subTopic)
              {
                  foreach (var item in postsByTopic[topicKeys.Key])
                  {
                      questionNumber++;
                      maximumTopicPages++;
                      threadsByTopic.Add(item);//Adds All DisplayAllTables objects
                      //if there are 20 add a button.
                      if (maximumTopicPages == 20)
                      {
                          tPages.Add(tPage++.ToString(), threadsByTopic);//Add a number to the page each time, with a DisplayTable object  
                          //new Button
                          Button pgs = new Button();//Create New Topic
                          pgs.Width = 20;                        
                          pgs.Command += obtainTopicsPerPage_Click;
                          pgs.CommandName = tPage.ToString();
                          pgs.Text = tPage.ToString();
  
                          btns.Add(tPage.ToString());
                          buttons.Add(pgs);
                          maximumTopicPages = 0;
                          threadsByTopic.Clear();
                      }
  
                  }//number of questions per page
                  if (!tPages.ContainsKey((questionNumber / 20).ToString()))
                  {
                      tPages.Add((questionNumber / 20).ToString(), threadsByTopic);//If A button is missing add it.
                  }
              } 
  </code></pre>
  
  <p>Way the buttons are added to the table:</p>
  
  <pre><code>    void MyButtonTable()
  {
  
      TableRow myTableRow = new TableRow();
           HtmlForm form = new HtmlForm();
  
      form.Attributes.Add("runat", "server");
  
  
      Page.Controls.Add(form);
  
      foreach (var item in buttons)
      {
          TableCell myTableCell = new TableCell();
          form.Controls.Add(item);
          myTableCell.Controls.Add(item);
          myTableRow.Cells.Add(myTableCell);
  
      }
  
      Table2.Rows.Add(myTableRow);
      Page.Controls.Add(Table2);
  }
  </code></pre>