簡體   English   中英

覆蓋Page_PreInit時的編譯器錯誤

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM