title: better way to create dynamic buttons in asp .net / c# that can access dynamic textboxes and remember their former values?
body: <p>Here's the thing, I now have a working prototype of what I want and it seems to work perfectly for what I want to do.  With that said, this cannot POSSIBLY be the best way to do it.  And by best I mean both in terms of program simplicity but also HOW i actually coded it.  Anyways first a description of what the page should do:</p>
  
  <p>1) the page contains a textbox (Textbox1), a placeholder (Placeholder1), and a button (Button1)</p>
  
  <p>the code for this is simple:</p>
  
  <pre><code>&lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt;
  
  &lt;asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /&gt;
  
  &lt;hr /&gt;
  &lt;asp:PlaceHolder ID="PlaceHolder1" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
  &lt;hr /&gt;
  </code></pre>
  
  <p>2) the user can put a number into the textbox (lets say 99) and click the button.  the placeholder will then be filled with 99 textboxes with a "save" button next to each one
  3)the user can edit any textbox and click save next to it and the function that is then called by the button click event needs to be able to have access to A) the textboxes current value, B) the textboxes old value, C) any amount of meta data</p>
  
  <p>here is my current working solution:</p>
  
  <pre><code>    protected void Page_Load(object sender, EventArgs e)
      {
          createStuff(false);
  
      }
  
      void test(object sender, EventArgs e)
      {
          Button btn = (Button)sender;
          Response.Write(btn.ID + "Clicked" + "&lt;br /&gt;");
          Response.Write("Metadata: " + Session[btn.ID + "a"] + "&lt;br /&gt;");
          Response.Write("Old text: " + Session[btn.ID + "b"] + "&lt;br /&gt;");
  
          TextBox txtBlah = (TextBox)PlaceHolder1.FindControl("txt"+btn.ID);
          Response.Write("New text: " + txtBlah.Text + "&lt;br /&gt;");
  
          Session[btn.ID + "b"] = txtBlah.Text;
  
      }
  
      protected void Button1_Click(object sender, EventArgs e)
      {
          Session["num"] = TextBox1.Text;
          createStuff(true);
      }
  
      private void createStuff(bool btnClick)
      {
          PlaceHolder1.Controls.Clear();
  
          int numBtn = Convert.ToInt32(Session["num"]);
  
          for (int i = 0; i &lt; numBtn; i++)
          {
              TextBox txtTemp = new TextBox();
              txtTemp.Text = "old" + i;
              txtTemp.ID = "txt" + "btn" + i;
              PlaceHolder1.Controls.Add(txtTemp);
  
              Button btn = new Button();
              btn.Text = "btn" + i;
              btn.ID = "btn" + i;
              btn.Click += new EventHandler(test);
              PlaceHolder1.Controls.Add(btn);
  
              Session[btn.ID + "a"] = "meta" + i;
  
              if (btnClick)
              {
              Session[btn.ID + "b"] = txtTemp.Text;
              }
  
              PlaceHolder1.Controls.Add(new LiteralControl("&lt;br /&gt;"));
          }
  
  
      }
  </code></pre>
  
  <p>I think this is actually pretty decent and I am very happy with it. But, I know there have to be some improvements i can make since some of the tutorials on how the ASP postback system and object model or whatever its called are even now confusing me some.</p>
  
  <p>any help or even just ideas are helpful thanks.</p>