title: Trouble deleting dynamically created buttons from form
body: <p>I have a website that takes an email, runs a powershell script, gets the output from that script and dynamically creates buttons based on that output. My trouble is that when you hit the back button and go through it again, the old button(s) still remain, and still dynamically create more buttons on top of that, resulting in duplicate buttons, and users from on email address accessing results from other email addresses.</p>
  
  <p>I cannot, however, figure out how to get rid of the button once the page changes. 
  I have tried creating the buttons dynamically into a panel instead but Panel.Clear is not working for me either, nor is Panel.Dispose or Form.Dispose.</p>
  
  <p>Here's the flow of the site:
  WebForm1>Accounts(in code WebForm4)>WebForm2>WebForm3</p>
  
  <p>Page 2 C#:</p>
  
  <pre><code>namespace ActiveDirectoryReset
  {
  public partial class WebForm4 : System.Web.UI.Page
  {
      public static string user; //this comes from what is returned by first 
  page
      public static string toEmail; //email from previous page
      public static string key; //irrelevant
  
  
  
      protected void Page_Load(object sender, EventArgs e)
      {
  
  
  
          int id = 1;
          int eventid = 0;
  
  
  
  
          //makes happy little buttons for each account associated with email 
  provided//
  
          System.Web.UI.HtmlControls.HtmlForm form = form1;
  
          Controls.Add(form);
  
          foreach (var account in WebForm1.accounts)
              {
  
  
                  eventid = id;
                  id++;
  
                  System.Web.UI.WebControls.Button button = new System.Web.UI.WebControls.Button();
  
                  button.ID = "btn" + eventid;
                  button.CssClass = "button";
                  button.Click += new EventHandler(this.selectUser);
                  button.Text = account;
  
                  button.Font.Name = "Segoe UI";
  
                  form.Controls.Add(button);
  
  
              }
  
  
  
  
  
  
  
      }
      //called after user is selected (dynamically created button is clicked)//
  
      public void selectUser(Object sender, EventArgs e)
      {
          System.Web.UI.WebControls.Button buttonClicked = (System.Web.UI.WebControls.Button)sender;
          user = buttonClicked.Text;
          WebForm2.hasBegun = true;
  
          generateKey();
          sendEmail();
  
          Server.Transfer("WebForm2.aspx");
  
  
  
      }
  
  
  
      //creates and sends email//
       public void sendEmail()
          {
  
  
              System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
              message.To.Add(toEmail);
              message.Subject = "Password Reset [[WORK IN PROGRESS]]";
              message.From = new System.Net.Mail.MailAddress("email@example.com");
              message.Body = "Hello, &lt;br /&gt;&lt;br /&gt;This is an email regarding your request to have your password changed for Active Directory account: " + user + ". If you did not request this, please disregard and contact your administrator. &lt;br /&gt;&lt;h3&gt;Your key is:&lt;/h3&gt;&lt;br /&gt;&lt;h1&gt;" + key + "&lt;/h1&gt;&lt;br /&gt;To continue and reset your password, go back to the webpage and enter the key above.";
  
  
              message.IsBodyHtml = true;
              System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("xxxxxx");
  
  
              smtp.Send(message);
  
  
          }
  
  
      //generates key used to verity email//
      public void generateKey()
      {
          var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
          var stringChars = new char[16];
          var random = new Random();
  
          for (int i = 0; i &lt; stringChars.Length; i++)
          {
              stringChars[i] = chars[random.Next(chars.Length)];
          }
          key = new String(stringChars);
      }
  //this is a cancel button that goes back to first page//
      protected void Button1_Click(object sender, EventArgs e)
      {
          WebForm2.hasBegun = false;
  
  
  
          Server.Transfer("WebForm1.aspx");
      }
  }
  }
  </code></pre>