title: Dynamic Event Handler not Firing
body: <p>I want to create an amount of controls dynamically based on a number the user enters into a textbox. This part I have working fine, but I also need the dynamically created textboxes to have event handlers of their own, so the user can enter a number into them, and have more controls created.</p>
  
  <p>My problem is that the event handler I've setup for these controls to use does not fire. Maybe I'm not understanding the ASP.NET page life cycle correctly, but I am currently having the dynamic controls generated inside the OnInit event (before I was generating them inside the TextChanged event of the very first textbox, but switched to using the OnInit event based on advice from Oded I found here: <a href="https://stackoverflow.com/questions/3927181/dynamically-added-event-handler-not-firing">Dynamically Added Event Handler Not Firing</a>). </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I removed the code I originally posted, because this post would just be too long otherwise.</p>
  
  <p>I'm going to post my entire .aspx code and the code behind here, so you guys know this is exactly what I'm looking at. Again, the dynamic TextBox that gets generated by this code, when text is changed inside it, does not fire the event handler that is bound to it, and it simply disappears. Interesting to note - I think a postback actually does occur when you change the text, but it doesn't fire the event handler...</p>
  
  <p><strong>ASPX front end:</strong></p>
  
  <pre><code>&lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;Untitled Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div id="dynamicControlDiv"&gt;
              &lt;asp:Label ID="lblEnter" runat="server" Text="Enter the amount of textboxes you want:"&gt;&lt;/asp:Label&gt;
              &lt;asp:TextBox ID="txtEnter" runat="server" AutoPostBack="true"&gt;&lt;/asp:TextBox&gt;
              &lt;asp:Label ID="lblConfirm" runat="server" Text=""&gt;&lt;/asp:Label&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>CODE BEHIND:</strong></p>
  
  <p>Partial Class _Default
      Inherits System.Web.UI.Page</p>
  
  <pre><code>Dim numOfDesiredControls As Int16
  
  Protected Sub txtEnter_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEnter.TextChanged
  
      Try
          numOfDesiredControls = Convert.ToInt16(txtEnter.Text)
          If Not numOfDesiredControls = 0 Then
              For i As Int16 = 1 To numOfDesiredControls
                  Dim txtTest As New TextBox
                  txtTest.Text = "dynamicTextBox"
                  txtTest.ID = "dynamicTextBox" + i.ToString
                  txtTest.AutoPostBack = True
                  Form.Controls.Add(txtTest)
                  AddHandler txtTest.TextChanged, AddressOf dynamicEventHandler
              Next
          End If
      Catch ex As Exception
  
      End Try
  End Sub
  
  Protected Sub dynamicEventHandler(ByVal sender As Object, ByVal e As System.EventArgs)
      ' If the event handler gets fired, reflect this by changing the text of lblConfirm
      lblConfirm.Visible = True
      lblConfirm.Text = "Event handler fired!"
  End Sub
  
  End Class
  </code></pre>
  
  <p>Try making your own project with this code, and see if you get the same problems I am - the dynamically created TextBox posts back, but does not fire its event handler, and then the TextBox disappears from the page... Thank you again for the help!</p>