ASP.NET 4.0 GridView当使用Unity 2.0 http模块时,OnRowEditing事件不触发 [英] ASP.NET 4.0 GridView OnRowEditing events not firing when using Unity 2.0 http module

查看:330
本文介绍了ASP.NET 4.0 GridView当使用Unity 2.0 http模块时,OnRowEditing事件不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET网站表单使用母版页。它使用Unity作为我的IoC容器。我创建了一个HTTP模块来构建容器使用我在网上找到的几个教程。我需要依赖注入为用户控件工作,我能够得到这个工作的唯一方法是挂钩到Pages PreInit事件,从下面的代码可以看出。

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.

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

}



并控制处理依赖注入的所有继承关闭实现

My pages and controls all inherit off base implementations which handle the dependency injection e.g.

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

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

我的依赖注入是按计划工作,但是当我使用GridViews页面OnRowEditing等命令不触发GridView。它就好像事件没有挂上。我已在HTML中设置事件如下。

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.

<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。任何人都可以说明为什么事件不发射?是我的Unity http模块导致这种情况发生通过删除事件接线?我可以得到这个工作没有问题在一个基本的例子,没有涉及IOC。

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.

任何帮助将非常感激。

感谢

Matt

推荐答案

在PreLoad上移动构建应该起作用,因为ViewState 在PreLoad事件之前加载

Moving build-up on PreLoad should work, because ViewState is loaded before the PreLoad event

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

这篇关于ASP.NET 4.0 GridView当使用Unity 2.0 http模块时,OnRowEditing事件不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆