title: ASP.NET 4.0 GridView OnRowEditing events not firing when using Unity 2.0 http module
body: <p>I have an ASP.NET web forms site using Master Pages.  It is using Unity as my IoC container.  I have created an HTTP Module to build up the container using a couple of tutorials i found online.  I need the dependency injection to work for User Controls and the only way i was able to get this to work was to hook into the Pages PreInit event as can be seen from the code below.</p>
  
  <pre><code>public class UnityHttpModule : IHttpModule
  {
      public void Init(HttpApplication context)
      {
          context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
      }
  
      public void Dispose() { }
  
      private void OnPreRequestHandlerExecute(object sender, EventArgs e)
      {
          IHttpHandler currentHandler = HttpContext.Current.Handler;
          HttpContext.Current.Application.GetContainer().BuildUp(
                              currentHandler.GetType(), currentHandler);
  
          // User Controls are ready to be built up after page initialization is complete
          var currentPage = HttpContext.Current.Handler as Page;
          if (currentPage != null)
          {
              currentPage.PreInit += Page_PreInit;
          }
      }
  
      // Build up each control in the page's control tree
      private void Page_PreInit(object sender, EventArgs e)
      {
          var currentPage = (Page)sender;
  
          BuildUp(currentPage);
  
          BuildUpMaster(currentPage.Master);
  
          BuildUpControls(currentPage.Controls);
      }
  
  
      private void BuildUp(object o)
      {
          HttpContext.Current.Application.GetContainer().BuildUp(o.GetType(), o);
      }
  
      private void BuildUpControls(ControlCollection controls)
      {
          foreach (Control c in controls)
          {
              if (c is UserControl)
                  BuildUp(c);
  
              BuildUpControls(c.Controls);
          }
      }
  
      private void BuildUpMaster(MasterPage page)
      {
          if (page != null)
          {
              BuildUp(page);
              BuildUpMaster(page.Master);
          }
      }
  
  }
  </code></pre>
  
  <p>My pages and controls all inherit off base implementations which handle the dependency injection e.g. </p>
  
  <pre><code>public class MyBaseUserControl : UserControl
  {
      [Dependency]
      public IMyServiceProvider MyService { get; set; }
  }
  
  public class MyPage : Page
  {
      [Dependency]
      public IMyServiceProvider MyService { get; set; }
  }
  </code></pre>
  
  <p>My Dependency Injection is working as planned, however when I use GridViews on my pages the OnRowEditing etc commands dont fire for the GridView.  Its as if the events arent hooked up.  I have set the events in the HTML as follows.</p>
  
  <pre><code>&lt;asp:GridView ID="gvComplaintCategory" runat="server" AutoGenerateColumns="False" DataKeyNames="ComplaintCategoryID" BackColor="#FFFFFF" GridLines="None" 
          CellPadding="2" CellSpacing="2" AllowPaging="True" PageSize="8" ShowFooter="true" 
          OnRowCommand="gvComplaintCategory_OnRowCommand" 
          OnRowEditing="gvComplaintCategory_OnRowEditing" 
          OnRowCancelingEdit="gvComplaintCategory_OnRowCancelingEdit"&gt;
  
                  &lt;asp:TemplateField&gt;
                      &lt;HeaderTemplate&gt;Complaint Category Name&lt;/HeaderTemplate&gt;
                      &lt;EditItemTemplate&gt;
                         &lt;asp:TextBox ID="txtComplaintCategoryEdit" runat="server" CssClass="textbox" Text='&lt;%# DataBinder.Eval(Container.DataItem, "ComplaintCategoryName") %&gt;'&gt;&lt;/asp:TextBox&gt;
                          &amp;nbsp;
                          &lt;asp:RequiredFieldValidator ID="rfvComplaintCategoryEdit" runat="server" ControlToValidate="txtComplaintCategory" Text="*" CssClass="RequiredFieldValidator" ValidationGroup="dgComplaintCategory"&gt;&lt;/asp:RequiredFieldValidator&gt;
                      &lt;/EditItemTemplate&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="lblComplaintCategory" runat="server" CssClass="label" Text='&lt;%# DataBinder.Eval(Container.DataItem, "ComplaintCategoryName") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;FooterTemplate&gt;
                         &lt;asp:TextBox ID="txtComplaintCategoryNew" runat="server" CssClass="textbox"&gt;&lt;/asp:TextBox&gt;
                          &amp;nbsp;
                          &lt;asp:RequiredFieldValidator ID="rfvComplaintCategoryNew" runat="server" ControlToValidate="txtComplaintCategoryNew" Text="*" CssClass="RequiredFieldValidator" ValidationGroup="dgComplaintCategory"&gt;&lt;/asp:RequiredFieldValidator&gt;
                      &lt;/FooterTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:Button ID="btnComplaintCategoryUpdate" runat="server" CssClass="button" CommandName="Update" Text="Update" CausesValidation="true" ValidationGroup="dgComplaintCategory"/&gt;
                          &amp;nbsp;
                          &lt;asp:Button ID="btnComplaintCategoryDelete" runat="server" CssClass="button" CommandName="Delete" Text="Delete" CausesValidation="false" ValidationGroup="dgComplaintCategory"/&gt;
                          &amp;nbsp;
                          &lt;asp:Button ID="btnComplaintCategoryCancel" runat="server" CssClass="button" CommandName="Cancel" Text="Cancel" CausesValidation="false" ValidationGroup="dgComplaintCategory"/&gt;
                      &lt;/EditItemTemplate&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Button ID="btnComplaintCategoryEdit" runat="server" CssClass="button" CommandName="Edit" Text="Edit" CausesValidation="false" ValidationGroup="dgComplaintCategory"/&gt;
                      &lt;/ItemTemplate&gt;
                      &lt;FooterTemplate&gt;
                          &lt;asp:Button ID="btnComplaintCategoryAdd" runat="server" CssClass="button" CommandName="Add" Text="Add" CausesValidation="true" ValidationGroup="dgComplaintCategory"/&gt;
                      &lt;/FooterTemplate&gt;
                  &lt;/asp:TemplateField&gt;  
              &lt;/Columns&gt;  
      &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>I also have autoeventwireup set to true on the pages and master page.  Can anyone shed any light on why the events are not firing?  Is my Unity http module causing this to happen by dropping the event wiring?  I can get this to work no problem in a basic example with no IoC involved.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Thanks</p>
  
  <p>Matt</p>