[英]Compiler error when overriding Page_PreInit
我試圖重寫Page_PreInit
我類內部功能_Default
從繼承Page
。 但是,當我嘗試編譯時,出現以下錯誤:
'_Default.Page_PreInit(object,System.EventArgs)':找不到適合的方法來覆蓋
這是我的代碼:
public partial class _Default : Page
{
protected override void Page_PreInit(object sender, EventArgs e)
{
// Todo:
// The _Default class overrides the Page_PreInit method and sets the value
// of the MasterPageFile property to the current value in the
// selectedLayout session variable.
MasterPageFile = Master.Session["selectedLayout"];
}
...
}
該Page
類聲明了一個名為公共事件PreInit
並命名為受保護的虛擬方法OnPreInit
(這只是引發PreInit
事件)。 因此,您有兩個選擇。
選項1(推薦):覆蓋OnPreInit
:
protected override void OnPreInit(EventArgs e)
{
// Set the master page here...
base.OnPreInit(e);
}
調用base.OnPreInit(e)
以便頁面照常引發PreInit
事件。
選項2:創建一個名為Page_PreInit
的方法。 只要您沒有在@Page
指令或Web.config中將AutoEventWireup
設置為False
,ASP.NET就會自動將此方法綁定到PreInit
事件。
private void Page_PreInit(object sender, EventArgs e)
{
// Set the master page here...
}
如果選擇此選項,請不要調用base.OnPreInit
,否則將導致無限遞歸。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.