我有一個使用母版頁的ASP.NET web表單站點。它使用Unity作爲我的IoC容器。我創建了一個HTTP模塊來使用我在網上找到的幾個教程構建容器。我需要依賴注入來爲用戶控件工作,並且我能夠實現這個功能的唯一方法就是掛入Pages PreInit事件中,如下面的代碼所示。使用Unity 2.0時ASP.NET 4.0 GridView OnRowEditing事件不會觸發http模塊
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; }
}
我的依賴注入正在按計劃進行,但是當我用GridView的在我的網頁上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>
<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>
<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"/>
<asp:Button ID="btnComplaintCategoryDelete" runat="server" CssClass="button" CommandName="Delete" Text="Delete" CausesValidation="false" ValidationGroup="dgComplaintCategory"/>
<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參與的基本示例中,我可以使這個工作沒有問題。
任何幫助將不勝感激。
感謝
馬特