【问题标题】:ASP.NET 4.0 GridView OnRowEditing events not firing when using Unity 2.0 http module使用 Unity 2.0 http 模块时,ASP.NET 4.0 GridView OnRowEditing 事件未触发
【发布时间】:2011-01-31 15:03:38
【问题描述】:

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

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

}

我的页面和控件都继承了处理依​​赖注入的基本实现,例如

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

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

我的依赖注入正在按计划工作,但是当我在我的页面上使用 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。谁能解释为什么事件没有触发?我的 Unity http 模块是否通过删除事件连接而导致这种情况发生?在一个不涉及 IoC 的基本示例中,我可以让它毫无问题地工作。

任何帮助将不胜感激。

谢谢

马特

【问题讨论】:

    标签: asp.net gridview c#-4.0 unity-container


    【解决方案1】:

    原因是您在控件加载 ViewState 之前对其进行了修改。

    在 PreLoad 上移动构建应该可以工作,因为 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      相关资源
      最近更新 更多