title: Dynamic button command event does not fire
body: <p>Actually, I'm developing dynamic template using asp.net and c#.<br />
  i have one main aspx page and inside this page i have an usercontrol page. inside  my usercontrol I should get the data from XML file then dynamically create a table inside the listview which each row include an edit and delete buttons at the last column as you can see in below image.</p>
  
  <p><img src="https://i.stack.imgur.com/MSffT.jpg" alt="enter image description here"></p>
  
  <p>I have create the table inside the <code>ItemDataBound</code> function as you can see below:</p>
  
  <pre><code>protected void lv_Uc_Module_ItemDataBound(object sender, ListViewItemEventArgs e)
  {  
      if (e.Item.ItemType == ListViewItemType.DataItem &amp;&amp; hid_SaveClicked.Value != "1")
      {
          XmlDocument xDocRead = new XmlDocument();
          xDocRead.Load(Server.MapPath("ModuleTemp.xml"));
          string xModuleName = hid_ChooseModule.Value;
          XmlNode xColCounter;
          TableRow tr_DataBound = new TableRow();
          TableRow tr_Lv_Header = new TableRow();
  
          if (!"".Equals(hid_ChooseModule.Value))
          {
              xColCounter = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/List");
          }
          else
          {
              xModuleName = xDocRead.SelectSingleNode("ModuleTemp").FirstChild.Name;
              xColCounter = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/List");
          }
          int pkCounter = 0, nonPkCounter = 0, colCount = xColCounter.ChildNodes.Count;
          string[] primaryKey = new string[30];
          string[] nonPrimaryKey = new string[colCount + 1];
  
          for (int i = 1; i &lt;= colCount; i++)
          {
              if (xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Edit/TableColumn" + i).Attributes.GetNamedItem("IsPrimaryKey").Value == "Y")
              {
                  primaryKey[pkCounter] = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Edit/TableColumn" + i).Attributes.GetNamedItem("Name").Value;
                  pkCounter++;
              }
              else
              {
                  nonPrimaryKey[nonPkCounter] = xDocRead.SelectSingleNode("ModuleTemp/" + xModuleName + "/Edit/TableColumn" + i).Attributes.GetNamedItem("Name").Value;
                  nonPkCounter++;
              }
          }
  
          System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
  
          for (int i = 0; i &lt; pkCounter + nonPkCounter; i++)
          { 
              dataBoundTemp[rowCounter, i] = rowView[i].ToString();
          }
          rowCounter++;
  
          TableCell tc_Lv_Header = new TableCell();
          if (!"".Equals(hid_ChooseModule.Value))
          {
              if (tempHeader == 0)
              {
                  tempHeader++;
                  for (int i = 1; i &lt;= pkCounter + nonPkCounter; i++)
                  {
                      tc_Lv_Header = new TableCell();
                      Label lb = new Label();
  
                      if (i &lt;= pkCounter)
                      {
                          lb.Text = primaryKey[i - 1];
                      }
                      else
                      {
                          lb.Text = nonPrimaryKey[i - pkCounter - 1];
                      }
  
                      lb.ID = "lb" + i + "_Table_IphContainer";
                      tc_Lv_Header.Controls.Add(lb);
                      tc_Lv_Header.CssClass = "th_ItemTemplate";
                      tr_Lv_Header.Cells.Add(tc_Lv_Header);
                  }
  
                  tr_Lv_Header.Cells.Add(tc_Lv_Header);
  
                  table_Header.Rows.Add(tr_Lv_Header);
                  PlaceHolder th_Ph_Lv_Hedear = (PlaceHolder)lv_Uc_Module.FindControl("th_Ph_Lv_header");
                  th_Ph_Lv_Hedear.Controls.Add(table_Header);
              }
          }
          TableCell tc_DataBound = new TableCell();
  
          if (editTempSelection == e.Item.DataItemIndex)
          {
              for (int i = 1; i &lt;= pkCounter + nonPkCounter; i++)
              {
                  //tc_DataBound = new TableCell();
                  tc_DataBound = new TableCell();
                  TextBox tb = new TextBox();
                  //(TextBox)e.Item.FindControl("td_EditTemp" + i);
                  tb.Width = 110;
                  tb.Text = rowView[i - 1].ToString();
                  tb.ID = "td_EditTemp" + i + "_" + e.Item.DataItemIndex;
                  tc_DataBound.Controls.Add(tb);
                  tc_DataBound.CssClass = "td_Tb_ItemTemplate";
                  tr_DataBound.Cells.Add(tc_DataBound);
              }
          }
          else
          {
              for (int i = 1; i &lt;= pkCounter + nonPkCounter; i++)
              {
                  tc_DataBound = new TableCell();
                  Label lb = new Label();
                  //(Label)e.Item.FindControl("lb_ItemTemp" + i);
                  lb.Text = rowView[i - 1].ToString();
                  lb.ID = "lb_ItemTemp" + i + "_" + e.Item.DataItemIndex;
                  tc_DataBound.Controls.Add(lb);
                  tc_DataBound.CssClass = "td_Lb_ItemTemplate";
                  tr_DataBound.Cells.Add(tc_DataBound);
              }
          }
          // add button to the columns
          if (editTempSelection != e.Item.DataItemIndex)
          {
              tc_DataBound = new TableCell();
              Button btn_Edit = new Button();
              btn_Edit.ID = "btn_Edit";
              btn_Edit.CommandName = "Edit";
              btn_Edit.CssClass = "btn_Edit";
              //btn_Edit.CausesValidation = true;
              tc_DataBound.Controls.Add(btn_Edit);
              tr_DataBound.Cells.Add(tc_DataBound);
  
              tc_DataBound = new TableCell();
              Button btn_Delete = new Button();
              btn_Delete.ID = "btn_Delete";
              btn_Delete.CommandName = "Delete";
              btn_Delete.CssClass = "btn_Delete";
              btn_Delete.CausesValidation = false;
              btn_Delete.OnClientClick = "return confirm('Are you sure you want to delete this item?');";
              //btn_Delete.Command += new CommandEventHandler(lv_Uc_Module_ItemDeleting);
              //btn_Delete.CommandName = e.Item.DataItemIndex.ToString();
              tc_DataBound.Controls.Add(btn_Delete);
              tr_DataBound.Cells.Add(tc_DataBound);
          }
          else
          {
              tc_DataBound = new TableCell();
              Button btn_Update = new Button();
              btn_Update.ID = "btn_Update";
              //btn_Update.CommandName = "Update";
              btn_Update.CssClass = "btn_Update";
              //btn_Update.ValidationGroup = "VGEditTmp";
              //btn_Update.OnClientClick = "alert('hi');";
              btn_Update.Command += new CommandEventHandler(lv_Uc_Module_ItemUpdating);
              btn_Update.CommandName = e.Item.DataItemIndex.ToString();
              tc_DataBound.Controls.Add(btn_Update);
              tr_DataBound.Cells.Add(tc_DataBound);
              hid_SaveClicked.Value = "1";
  
              tc_DataBound = new TableCell();
              Button btn_Cancel = new Button();
              btn_Cancel.ID = "btn_Cancel";
              btn_Cancel.CommandName = "Cancel";
              btn_Cancel.CssClass = "btn_Cancel";
              btn_Cancel.CausesValidation = false;
              tc_DataBound.Controls.Add(btn_Cancel);
              tr_DataBound.Cells.Add(tc_DataBound);
              tr_DataBound.CssClass = "tr_Edit_ItemTemplate";
          }
  
          // add columns to the row
          tr_DataBound.Cells.Add(tc_DataBound);
          table_Lv_ItemTemplate = (Table)e.Item.FindControl("Table_Lv_ItemTemplate");
          table_Lv_ItemTemplate.Rows.Add(tr_DataBound);
          //"lb_ItemTemp" + i + "_" + e.Item.DataItemIndex;     "lb_ItemTemp" + rowCounter + "_" + e.Item.DataItemIndex
      }
  }
  </code></pre>
  
  <p>When the user click on the edit button it has to change the labels of the selected row to textbox, as you can see in below picture:</p>
  
  <p><img src="https://i.stack.imgur.com/ZoBhW.jpg" alt="enter image description here"></p>
  
  <p>My listview code is as below:</p>
  
  <pre><code>&lt;asp:ListView ID="lv_Uc_Module" runat="server"
                      OnItemEditing="lv_Uc_Module_ItemEditing" 
                      OnItemDeleting="lv_Uc_Module_ItemDeleting"
                      OnItemCanceling="lv_Uc_Module_ItemCanceling" 
                      OnItemDataBound="lv_Uc_Module_ItemDataBound"
                      OnSorting="lv_Uc_Module_Sorting"&gt;
  
                          &lt;LayoutTemplate&gt;
                              &lt;asp:Table runat="server" ID="table_Lv_Layout"&gt;
                                  &lt;asp:TableRow runat="server" ID="tr_Table_Layout"&gt;
                                      &lt;asp:TableCell runat="server" ID="td_Table_Layout"&gt;
                                          &lt;asp:Table runat="server" ID="itemPlaceholderContainer"&gt;
                                              &lt;asp:TableRow runat="server" ID="tr_Table_IphContainer"&gt;
  
                                                  &lt;asp:TableHeaderCell runat="server"&gt;
                                                      &lt;asp:PlaceHolder ID="th_Ph_Lv_header" runat="server"&gt;&lt;/asp:PlaceHolder&gt;
                                                  &lt;/asp:TableHeaderCell&gt;
                                              &lt;/asp:TableRow&gt;
                                              &lt;asp:TableRow runat="server"&gt;
                                                  &lt;asp:TableCell runat="server"&gt;
  
                                                      &lt;asp:PlaceHolder runat="server" ID="itemPlaceholder" /&gt;
  
                                                  &lt;/asp:TableCell&gt;
  
                                              &lt;/asp:TableRow&gt;
                                          &lt;/asp:Table&gt;
                                      &lt;/asp:TableCell&gt;
                                  &lt;/asp:TableRow&gt;
                                  &lt;asp:TableRow runat="server" ID="tr_Validate_Table_Layout"&gt;
                                      &lt;asp:TableCell runat="server" ID="td_Validate_Table_Layout" HorizontalAlign="Center" BackColor="#CCCCCC"&gt;
                                          &lt;asp:ValidationSummary ID="ValidationSummary1" runat="server" HeaderText="You received the following errors:" ShowMessageBox="true" ShowSummary="false" ValidationGroup="VGEditTmp" /&gt;
                                          &lt;%--&lt;asp:ValidationSummary ID="ValidationSummary2" runat="server" HeaderText="You received the following errors:" ShowMessageBox="true" ShowSummary="false" ValidationGroup="VGInsertTmp" /&gt;--%&gt;
                                      &lt;/asp:TableCell&gt;
                                  &lt;/asp:TableRow&gt;
                              &lt;/asp:Table&gt;
                              &lt;br /&gt;
                              &lt;asp:DataPager ID="lv_DataPager" runat="server" PagedControlID="lv_Uc_Module" PageSize="25" OnPreRender="lv_DataPager_PreRender"&gt;
                                  &lt;Fields&gt;
                                      &lt;asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="true" ShowLastPageButton="true" FirstPageImageUrl="~/Images/First.png" LastPageImageUrl="~/Images/Last.png" NextPageImageUrl="~/Images/Next.png" PreviousPageImageUrl="~/Images/Previous.png" /&gt;
                                      &lt;asp:TemplatePagerField&gt;
                                          &lt;PagerTemplate&gt;
                                              &lt;span style="color:Blue;"&gt;
                                              &lt;/span&gt;
                                          &lt;/PagerTemplate&gt;
                                      &lt;/asp:TemplatePagerField&gt;
                                  &lt;/Fields&gt;
                              &lt;/asp:DataPager&gt;
                          &lt;/LayoutTemplate&gt;
  
                          &lt;ItemTemplate&gt;
                              &lt;asp:TableRow runat="server"&gt;
                                  &lt;asp:TableCell runat="server"&gt;
  
                                      &lt;asp:Table runat="server" ID="Table_Lv_ItemTemplate"&gt;&lt;/asp:Table&gt;
  
                                  &lt;/asp:TableCell&gt;
                                  &lt;asp:TableCell runat="server"&gt;
                                      &lt;asp:Button ID="btn_Edit" runat="server" CommandName="Edit" Text="" CssClass="btn_Edit" CausesValidation="True" Visible="false" /&gt;
                                      &lt;asp:Button ID="btn_Delete" runat="server" CommandName="Delete" Text="" CssClass="btn_Delete" CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this item?');" Visible="false" /&gt;
                                  &lt;/asp:TableCell&gt;
                              &lt;/asp:TableRow&gt;
                          &lt;/ItemTemplate&gt;
  </code></pre>
  
  <p>The problem is once the user click on the save button to save the change of the edited item to the database it never execute the <code>ItemUpdating</code> function. I have put below code at my <code>ItemDataBound</code> function to call the <code>ItemUpdating</code> function.</p>
  
  <pre><code> btn_Update.Command += new CommandEventHandler(lv_Uc_Module_ItemUpdating);
   btn_Update.CommandName = e.Item.DataItemIndex.ToString();
  </code></pre>
  
  <p>Please guide me how to get it done.
  I appreciate your consideration. </p>