2015-05-06 112 views
1

我目前遇到一個奇怪的問題。首先我的代碼被剪斷:System.Web.UI.Page覆蓋InitializeCulture並訪問MasterPage

// This is my base page class 
public class MyBasePage 
{ 
    protected override void InitializeCulture() 
    { 
     base.InitializeCulture(); 
     MasterPage master = Page.Master; // point1 
    } 
} 

// This is my page 
public partial class Default : MyBasePage 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     label1.Text = "Test"; // point2 
    } 
} 

通常,point2上的label1不是NULL。但是,如果我訪問InitializeCulture()事件(point1)中的masterpage,它是。有人解釋了爲什麼?

我想我必須找到另一個解決方法,這對我來說可以。但我想知道那裏發生了什麼。

回答

0

這是因爲母版頁和主題在初始化期間應用於頁面,並且在此之前調用InitializeCulture,所以它返回null。

+0

我想你missunderstood。 Page.Master不爲null。但是,如果在InitializeCulture中訪問主設備,那麼label_1將在Page_OnLoad中爲空。 – Undercover1989

+0

您不應首先訪問InitializeCulture中的MasterPage。它尚未填充,所以它沒有任何意義。 – kenik

0

我分析了ILSpy的行爲。問題是,主頁面沒有在InitializeCulture事件中初始化,如果被訪問,它將被初始化 - 但顯然不正確。

結論:永遠不要從InitializeCulture事件訪問masterpage - 它會弄亂頁面初始化過程。

這裏,只爲信息的.NET代碼初始化母版:

internal static MasterPage CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) 
{ 
    MasterPage masterPage = null; 
    if (masterPageFile == null) 
    { 
     if (contentTemplateCollection != null && contentTemplateCollection.Count > 0) 
     { 
      throw new HttpException(SR.GetString("Content_only_allowed_in_content_page")); 
     } 
     return null; 
    } 
    else 
    { 
     VirtualPath virtualPath = VirtualPathProvider.CombineVirtualPathsInternal(owner.TemplateControlVirtualPath, masterPageFile); 
     ITypedWebObjectFactory typedWebObjectFactory = (ITypedWebObjectFactory)BuildManager.GetVPathBuildResult(context, virtualPath); 
     if (!typeof(MasterPage).IsAssignableFrom(typedWebObjectFactory.InstantiatedType)) 
     { 
      throw new HttpException(SR.GetString("Invalid_master_base", new object[] 
      { 
       masterPageFile 
      })); 
     } 
     masterPage = (MasterPage)typedWebObjectFactory.CreateInstance(); 
     masterPage.TemplateControlVirtualPath = virtualPath; 
     if (owner.HasControls()) 
     { 
      foreach (Control control in owner.Controls) 
      { 
       LiteralControl literalControl = control as LiteralControl; 
       if (literalControl == null || Util.FirstNonWhiteSpaceIndex(literalControl.Text) >= 0) 
       { 
        throw new HttpException(SR.GetString("Content_allowed_in_top_level_only")); 
       } 
      } 
      owner.Controls.Clear(); 
     } 
     if (owner.Controls.IsReadOnly) 
     { 
      throw new HttpException(SR.GetString("MasterPage_Cannot_ApplyTo_ReadOnly_Collection")); 
     } 
     if (contentTemplateCollection != null) 
     { 
      foreach (string text in contentTemplateCollection.Keys) 
      { 
       if (!masterPage.ContentPlaceHolders.Contains(text.ToLower(CultureInfo.InvariantCulture))) 
       { 
        throw new HttpException(SR.GetString("MasterPage_doesnt_have_contentplaceholder", new object[] 
        { 
         text, 
         masterPageFile 
        })); 
       } 
      } 
      masterPage._contentTemplates = contentTemplateCollection; 
     } 
     masterPage._ownerControl = owner; 
     masterPage.InitializeAsUserControl(owner.Page); 
     owner.Controls.Add(masterPage); 
     return masterPage; 
    } 
}