マスタページを使用するASP.NET Webフォームサイトがあります。私はIoCコンテナとしてUnityを使用しています。私はHTTPモジュールを作成して、オンラインで見つけた2つのチュートリアルを使ってコンテナを構築しました。私はUser Controlsのために依存性注入が必要です。これを動作させるための唯一の方法は、以下のコードからわかるように、Pages PreInitイベントにフックすることでした。Unity 4.0 HTTPモジュールを使用しているときにASP.NET 4.0 GridView OnRowEditingイベントが起動しない
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>
<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を含まない基本的な例で問題なく動作させることができます。
ご協力いただければ幸いです。
おかげ
マット