ASP.NET 4.0 GridView OnRowEditing events do not fire when using Unity 2.0 http module

I have an ASP.NET web form site using master pages. It uses Unity as my IoC container. I created an HTTP module to create a container with a few tutorials that I found on the Internet. I need dependency injection to work with User Controls, and the only way I was able to get this to work was to hook into the Pages PreInit event, as seen in the code below.

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 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);
        }
    }

}

My pages and controls all inherit from the underlying implementations that handle dependency injection, for example.

public class MyBaseUserControl : UserControl
{
    [Dependency]
    public IMyServiceProvider MyService { get; set; }
}

public class MyPage : Page
{
    [Dependency]
    public IMyServiceProvider MyService { get; set; }
}

My Dependency Injection , , , GridViews , OnRowEditing .. GridView. . HTML .

<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">

                <asp:TemplateField>
                    <HeaderTemplate>Complaint Category Name</HeaderTemplate>
                    <EditItemTemplate>
                       <asp:TextBox ID="txtComplaintCategoryEdit" runat="server" CssClass="textbox" Text='<%# DataBinder.Eval(Container.DataItem, "ComplaintCategoryName") %>'></asp:TextBox>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="rfvComplaintCategoryEdit" runat="server" ControlToValidate="txtComplaintCategory" Text="*" CssClass="RequiredFieldValidator" ValidationGroup="dgComplaintCategory"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblComplaintCategory" runat="server" CssClass="label" Text='<%# DataBinder.Eval(Container.DataItem, "ComplaintCategoryName") %>'></asp:Label>
                    </ItemTemplate>
                    <FooterTemplate>
                       <asp:TextBox ID="txtComplaintCategoryNew" runat="server" CssClass="textbox"></asp:TextBox>
                        &nbsp;
                        <asp:RequiredFieldValidator ID="rfvComplaintCategoryNew" runat="server" ControlToValidate="txtComplaintCategoryNew" Text="*" CssClass="RequiredFieldValidator" ValidationGroup="dgComplaintCategory"></asp:RequiredFieldValidator>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:Button ID="btnComplaintCategoryUpdate" runat="server" CssClass="button" CommandName="Update" Text="Update" CausesValidation="true" ValidationGroup="dgComplaintCategory"/>
                        &nbsp;
                        <asp:Button ID="btnComplaintCategoryDelete" runat="server" CssClass="button" CommandName="Delete" Text="Delete" CausesValidation="false" ValidationGroup="dgComplaintCategory"/>
                        &nbsp;
                        <asp:Button ID="btnComplaintCategoryCancel" runat="server" CssClass="button" CommandName="Cancel" Text="Cancel" CausesValidation="false" ValidationGroup="dgComplaintCategory"/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="btnComplaintCategoryEdit" runat="server" CssClass="button" CommandName="Edit" Text="Edit" CausesValidation="false" ValidationGroup="dgComplaintCategory"/>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:Button ID="btnComplaintCategoryAdd" runat="server" CssClass="button" CommandName="Add" Text="Add" CausesValidation="true" ValidationGroup="dgComplaintCategory"/>
                    </FooterTemplate>
                </asp:TemplateField>  
            </Columns>  
    </asp:GridView>

autoeventwireup true . - , ? http- Unity , ? IoC.

.

Matt

+3
1

, , ViewState.

PreLoad , ViewState PreLoad

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;
        currentPage.PreLoad += Page_PreLoad;
    }
}

// Build up each control in the page control tree
private void Page_PreInit(object sender, EventArgs e)
{
    var currentPage = (Page)sender;

    BuildUp(currentPage);

    BuildUpMaster(currentPage.Master);
}

private void Page_PreLoad(object sender, EventArgs e)
{
    var currentPage = (Page)sender;

    BuildUpControls(currentPage.Controls);
}
+2

Source: https://habr.com/ru/post/1789007/


All Articles