title: ASP.NET dynamic Command Button event not firing
body: <p>I'm trying to create Command Buttons dynamically, but clicking the button in question doesn't seem to raise the corresponding CommandButton_Click event. I noticed that in the examples on SO a property is set for Button.OnCommand as well as the CommandName and CommandArgument but it isn't an option in intellisense.</p>
  
  <p>So the question is, what am I doing wrong here (code below without the OnCommand), is it accessed in some other way - if so, why do the examples I've found all show it as .OnCommand?</p>
  
  <p><strong>EDIT:</strong> Further to help, I have added the handler however the event is still not firing. The buttons reside in an UpdatePanel and are rebuilt on every postback (along with the handler). I have created a simplified example of what I'm doing which is shown below If the button event fires, it writes "EVENT FIRED" to the txtTestFired Textbox - suffice to say I have never seen that. This is really driving me nuts, any help is very gratefully received.</p>
  
  <p>.aspx file</p>
  
  <pre><code>&lt;form id="frmMain" runat="server"&gt;
      &lt;asp:ScriptManager ID="scmAddProducts" runat="server"&gt;
      &lt;/asp:ScriptManager&gt;
      &lt;asp:updatepanel runat="server"&gt;
          &lt;ContentTemplate&gt;
              &lt;asp:TextBox ID="txtProduct" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;br /&gt;
              &lt;asp:Button ID="btnAddItem" runat="server" Text="Add Line" /&gt;&amp;nbsp;
              &lt;asp:TextBox ID="txtTestFired" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;br /&gt;
              &lt;br /&gt;
              &lt;asp:Panel ID="pnlAddedLines" runat="server"&gt;&lt;/asp:Panel&gt;
          &lt;/ContentTemplate&gt;
      &lt;/asp:updatepanel&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>.aspx.vb file</p>
  
  <pre><code>Protected Sub btnAddItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddItem.Click
      Dim dtItems As New System.Data.DataTable
  
      If Session("Items") Is Nothing Then
          Dim dcColumn As New System.Data.DataColumn
          dcColumn.DataType = Type.GetType("System.String")
          dcColumn.ColumnName = "Product"
          dtItems.Columns.Add(dcColumn)
          Session("Items") = dtItems
      End If
  
      dtItems = CType(Session("Items"), System.Data.DataTable)
      Dim drRow As System.Data.DataRow
      drRow = dtItems.NewRow()
      drRow("Product") = txtProduct.Text
      dtItems.Rows.Add(drRow)
  
      Session("Items") = dtItems
      txtProduct.Text = ""
  End Sub
  Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
      If Not Session("Items") Is Nothing Then
          Dim dtItems As System.Data.DataTable = CType(Session("Items"), System.Data.DataTable)
          Dim iItemIndex As Integer = 1
          For Each drRow In dtItems.Rows
              Dim btnClose As New Button
              btnClose.ID = "btnClose" &amp; iItemIndex
              btnClose.CssClass = "formCloseButton"
              btnClose.Text = "X"
              AddHandler btnClose.Click, AddressOf Button_Clicked
              pnlAddedLines.Controls.Add(btnClose)
              btnClose = Nothing
  
              Dim txtProduct = New TextBox
              txtProduct.ID = "txtProduct" &amp; iItemIndex
              txtProduct.CssClass = "formText"
              txtProduct.Text = drRow("Product")
              txtProduct.Columns = "40"
              pnlAddedLines.Controls.Add(txtProduct)
              iItemIndex += 1
  
              Dim litHR = New Literal
              litHR.Text = "&lt;hr /&gt;"
              pnlAddedLines.Controls.Add(litHR)
              litHR = Nothing
          Next
      End If
  End Sub
  Private Sub Button_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)
      txtTestFired.Text = "EVENT FIRED"
  End Sub
  </code></pre>