title: getting TextBox Text before post back
body: <p>I'm building a user control that is a multiple text box generator.
  it gives you the ability to create as much textboxs as you need..</p>
  
  <p>My problem is that every time i'm using the btnAdd to add a new TextBox, the .Net is sending a post back to the server - which removes all the text inside the current text boxes..</p>
  
  <p>Is there a way that i can catch the text in the textbox before the callback in order to re-render the user control with the text inside ?</p>
  
  <p><strong>HTML part:</strong></p>
  
  <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeFile="MultipleTextBox.ascx.cs"
      Inherits="MultipleTextBox" %&gt;
  &lt;asp:ScriptManager runat="server" ID="scriptManager"&gt;
  &lt;/asp:ScriptManager&gt;
  &lt;asp:UpdatePanel ID="updatePanel" runat="server"&gt;
      &lt;contenttemplate&gt;
  &lt;div id="container" runat="server"&gt;
  &lt;/div&gt;
  
  &lt;asp:Button runat="server" Text="+" ID="btnAdd" OnClick="btnAdd_Click"&gt;&lt;/asp:Button&gt;
  &lt;asp:Button runat="server" Text="-" ID="btnRemove" onclick="btnRemove_Click"&gt;&lt;/asp:Button&gt;
  
  &lt;/contenttemplate&gt;
  &lt;/asp:UpdatePanel&gt;
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>public partial class MultipleTextBox : System.Web.UI.UserControl
  {
      /// &lt;summary&gt;
      /// The Session key for text box list
      /// &lt;/summary&gt;
      private readonly string SESSION_TEXTBOX_LIST = "textboxList";
  
      /// &lt;summary&gt;
      /// The Session key for MultipleTextBox name
      /// &lt;/summary&gt;
      private readonly string SESSION_NAME = "multipleTextBoxName";
  
      /// &lt;summary&gt;
      /// The List of TextBox instances
      /// &lt;/summary&gt;
      private List&lt;TextBox&gt; textBoxList;
  
      /// &lt;summary&gt;
      /// The basic name of this multiple text box
      /// &lt;/summary&gt;
      private string name;
  
      protected void Page_Load(object sender, EventArgs e)
      {                
          //disable the validation using does buttons
          btnAdd.CausesValidation = false;
          btnRemove.CausesValidation = false;
          //if it's not a post back - intialize the page
          if (!IsPostBack)
          {
              Session[SESSION_TEXTBOX_LIST] = null;
              Session[SESSION_NAME] = null;
              textBoxList = new List&lt;TextBox&gt;();
              Session[SESSION_TEXTBOX_LIST] = textBoxList;
              btnRemove.Visible = false;
              return;
          }
          //get the name
          if (Session[SESSION_NAME] != null)
              name = (string)Session[SESSION_NAME];
          //get the text box list
          if (Session[SESSION_TEXTBOX_LIST] != null)
              textBoxList = (List&lt;TextBox&gt;)Session[SESSION_TEXTBOX_LIST];
      }
  
      protected void btnAdd_Click(object sender, EventArgs e)
      {
          //add a new text box to the text box list
          textBoxList.Add(createTextBox());
          //show the text boxes
          ShowTextboxes(false);
          if (textBoxList.Count &gt; 0)
              btnRemove.Visible = true;
      }
  
      protected void btnRemove_Click(object sender, EventArgs e)
      {
          //show the text boxes
          ShowTextboxes(true);
          //set the visibility of the remove button
          if (textBoxList.Count == 0)
              btnRemove.Visible = false;
      }
  
      /// &lt;summary&gt;
      /// Set the buttons for this control
      /// &lt;/summary&gt;
      private void setButtons()
      {
          //set the visibility of the remove button
          if (textBoxList.Count == 0)
              btnRemove.Visible = false;
          else
              btnRemove.Visible = true;
      }
  
      /// &lt;summary&gt;
      /// Create a new TextBox
      /// &lt;/summary&gt;
      /// &lt;returns&gt;a new TextBox with the right name attribute&lt;/returns&gt;
      private TextBox createTextBox()
      {
          TextBox textbox = new TextBox();
          textbox.Attributes["name"] = name + "_" + (textBoxList.Count + 1);
          textbox.ID = name + "_" + (textBoxList.Count + 1);
          return textbox;
      }
  
      /// &lt;summary&gt;
      /// Show the TextBox instances
      /// &lt;/summary&gt;
      /// &lt;param name="isRemoveLast"&gt;true - use after clicking the remove button - will remove the last TextBox element. false - will show the whole TextBox list&lt;/param&gt;
      private void ShowTextboxes(bool isRemoveLast)
      {
          if (textBoxList == null || name == null)
              return;
          //if remove the last element - remove it
          if (isRemoveLast)
              textBoxList.RemoveAt(textBoxList.Count - 1);
          //add each text box to the container
          foreach (TextBox textBox in textBoxList)
          {
              //RequiredFieldValidator validator = new RequiredFieldValidator();
              //validator.ErrorMessage = "*required";
              //validator.ControlToValidate = textBox.ID;
              container.Controls.Add(textBox);
              //container.Controls.Add(validator);
              container.Controls.Add(new LiteralControl("&lt;br/&gt;"));
          }
      }
  
      /// &lt;summary&gt;
      /// Get or set the basic name of thei MultipleTextBox
      /// &lt;/summary&gt;
      public string Name
      {
          get
          {
              return this.name;
          }
          set
          {
              this.name = value;
          }
      }
  }
  </code></pre>
  
  <p><strong>P.S</strong>
  Would love for answer with pure C# and .Net. not JavaScript if possible..</p>