有母版页的内容页中的PreInit事件找不到控件的问题

最近碰上一个问题,需要在内容页中的preinit 事件访问控件,但是却会报错:未将对象引用设置到对象的实例。程序如下:
 
<%@Page Language="C#" MasterPageFile="~/MasterPage.master" Title="Page"%> 

 
    

Content

 
    

 
 

 
    protected override void OnPreInit(EventArgs e) 
    { 
                base.OnPreInit(e); 
                ContentPageLabel.Text = "Hello, World!"; 
    } 
 
在网上查了半天,终于找到了解决方法:
 
   protected override void OnPreInit(EventArgs e)
    { 
                  base.OnPreInit(e); 
        // 下面这一行很重要
        MasterPage master = this.Master;

        ContentPageLabel.Text = "Hello, World!"; 
    } 
网上说是需要先访问一下母版页,这样内容页的控件才能实例化,才能被我们访问到。

分享题目:有母版页的内容页中的PreInit事件找不到控件的问题
分享URL:http://pwwzsj.com/article/ishscs.html