title: Dynamically added button on dynamically created button click event not firing
body: <p>I have a button which is created from a MySQL query result. It looks like this:</p>
  
  <p>1 Product name |add to basket| </p>
  
  <p>And when I press the "add to basket" button, the product appears in a table on the right side(basket). Like this:</p>
  
  <p>1 Product name |remove|</p>
  
  <p>But for some reason, the <code>remove</code> button click-event removes all items from basket. When I press <code>add to basket</code> again, all items appear in the basket again. </p>
  
  <p>This is whole class. Thank you for your help</p>
  
  <pre><code>public partial class neworder : System.Web.UI.Page
  {   
      Basket basket;
      Table BasketTable;
      private int countRows = 0;
      private string distance;
      private string categories;
      ArrayList categoryList = new ArrayList();
      ArrayList productList = new ArrayList();
      private string products = "";
  
  
      public string Products
      {
          get { return products; }
          set { products = value; }
      }
  
      public string Categories
      {
          get { return categories; }
          set { categories = value; }
      }
      public string Distance
      {
          get { return distance; }
          set { distance = value; }
      }
  
      protected void Page_Load(object sender, EventArgs e)
      { 
      }
      protected void Page_Init(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              basket = new Basket();
              Session["basket"] = basket;
  
          }
          populateCategories();
          populateProducts();
          loadBasket(); // if this is comment then i items are added fine 
  //to basket but cant be remove, if its not commented then i can add 
  //only 2 items in basket
      }
  
      private void populateCategories()
      {
          using (MySqlConnection cn = new MySqlConnection(Static.MysqlServer1))
          {
              cn.Open();
              String strSQL = "SELECT category_name FROM categories;";
              MySqlCommand cmdPLT = new MySqlCommand(strSQL, cn);
              MySqlDataReader myReader;
              myReader = cmdPLT.ExecuteReader();
              while (myReader.Read())
              {
                  string cat = myReader.GetString(0);
                  categoryList.Add(cat);
                  categories += "&lt;a href=\"#"+cat+"\"&gt;" + cat + "&lt;/a&gt;&lt;br /&gt;";
              }
          }
      }
      private void populateProducts()
      {
  
          Panel1.Controls.Clear();
          string tem = "";
          foreach (string s in categoryList)
          {
              Table tableTitle = new Table();
              TableRow row1 = new TableRow();
              tableTitle.ID = s + "Title";
              tableTitle.CssClass = "rest2";
              Panel1.Controls.Add(tableTitle);
  
              TableCell cell2 = new TableCell();
              Label label2 = new Label();
              label2.Text = "&lt;a name="+s+"&gt;"+s+"&lt;/a&gt;";
              label2.ID = "TitleText_" + s;
              cell2.Controls.Add(label2);
              row1.Cells.Add(cell2);
              tableTitle.Controls.Add(row1);
  
              Table table = new Table();
              table.ID = s;
              table.CssClass = "rest";
              Panel1.Controls.Add(table);
  
              using (MySqlConnection cn = new MySqlConnection(Static.MysqlServer1))
              {
                  string name = "";
                  string size = "";
                  int id = -1;
                  double price = 0.0;
                  cn.Open();
                  String strSQL = "SELECT product_id, product_name, product_price, product_size FROM products join categories on categories.CATEGORY_ID = products.CATEGORY_ID where category_name ='" + s + "' order by product_order asc;";
                  MySqlCommand cmdPLT = new MySqlCommand(strSQL, cn);
                  MySqlDataReader myReader;
                  myReader = cmdPLT.ExecuteReader();
                  while (myReader.Read())
                  {                   
                      name = (string)myReader["product_name"];
                      id = (int)myReader["product_id"];
                      size = (string)myReader["product_size"];
                      price = (double)myReader["product_price"];
                      productList.Add(new Products(id, name, size, price));
                      GenerateRow(s, name, size, price, table, id);
                  }
                  if (!tem.Equals(""))
                  {
                      tem += "&lt;/table&gt;";
                  }
              }
              products += tem;
              tem = "";
          }
  
      }
      private void GenerateRow(string category, string name, string size, double price, Table table,int id )
      {
  
              TableRow row1 = new TableRow();
  
              //ROW 1 NAME
              TableCell cell1 = new TableCell();
              Label label1 = new Label();
              label1.Text = name;
              cell1.Style.Add("width", "300px !important");
              label1.ID = "NameText_" + category+id;
              cell1.Controls.Add(label1);
              row1.Cells.Add(cell1);
  
              //ROW 2 Size
              TableCell cell2 = new TableCell();
              Label label2 = new Label();
              cell2.Style.Add("width", "100px !important");
              label2.Text = size;
              label2.ID = "SizeText_" +category+ id;
              cell2.Controls.Add(label2);
              row1.Cells.Add(cell2);
  
              //ROW 1 PRICE
              TableCell cell3 = new TableCell();
              Label label3 = new Label();
              cell3.Style.Add("width", "60px !important");
              label3.Text = "" + price;
              label3.ID = "Price_" + category + id;
              cell3.Controls.Add(label3);
              row1.Cells.Add(cell3);
  
              //ROW 1 BUTTON
              TableCell cell4 = new TableCell();
              cell4.Style.Add("height", "25px !important");
              Button button = new Button();
              button.Text = "+";
              button.Click += button_Click;
              button.ID = ""+id;
              cell4.Controls.Add(button);
              row1.Cells.Add(cell4);
              table.Controls.Add(row1);
          }
  
      private void loadBasket()
      {
              Basket temp = (Basket)Session["basket"];
              BasketTable = new Table();
              BasketTable.ID = "basket";
              BasketTable.CssClass = "rest";
              PanelBasket.Controls.Clear();
              PanelBasket.Controls.Add(BasketTable);
  
  
          foreach (Products p in temp.ProductList)
          {
  
              TableRow row1 = new TableRow();
  
              //ROW 1 NAME
              TableCell cell1 = new TableCell();
              Label label1 = new Label();
              label1.Text = p.Name;
              cell1.Style.Add("width", "100px !important");
              label1.ID = p.Name;
              cell1.Controls.Add(label1);
              row1.Cells.Add(cell1);
  
              //ROW 2 Size
              TableCell cell2 = new TableCell();
              Label label2 = new Label();
              cell2.Style.Add("width", "40px !important");
              label2.Text = p.Size;
              label2.ID = p.Size;
              cell2.Controls.Add(label2);
              row1.Cells.Add(cell2);
  
              //ROW 1 PRICE
              TableCell cell3 = new TableCell();
              Label label3 = new Label();
              cell3.Style.Add("width", "60px !important");
              label3.Text = p.Price+"";
              label3.ID = p.Price + "";
              cell3.Controls.Add(label3);
              row1.Cells.Add(cell3);
  
              //ROW 1 BUTTON
              TableCell cell4 = new TableCell();
              cell4.Style.Add("height", "25px !important");
              Button remove = new Button();
              remove.Text = "-";
              remove.Click += new EventHandler(button99_Click);
              remove.ID = "remove" + p.Id;
              cell4.Controls.Add(remove);
              row1.Cells.Add(cell4);
  
             BasketTable.Controls.Add(row1);
          }
      }
  
      private void button_Click(object sender, EventArgs e)
      {  
          Button button = (Button)sender;
          string buttonId = button.ID;
          foreach (Products p in productList)
          {
              if (p.Id == Convert.ToInt32(buttonId))
              {
                  Basket temp = (Basket)Session["basket"];
                  temp.addPruduct(p);
                  Session["basket"] = temp;
                      loadBasket();
                  Label1.Text = temp.ProductList.Count + "";
              }
          }
      }
      protected void remove_Click(object sender, EventArgs e)
      {
          Label1.Text = "test";
      }
  }
  </code></pre>