Master.FindControl 给出 NullReferenceException

Master.FindControl giving NullReferenceException

我正在尝试这个

if (!IsPostBack)
    {
        Label theLabel = Master.FindControl("HeadingLabel") as Label;
        if (theLabel != null)
        {
            theLabel.Text = "Our Valuable Customers ";
        }
    }

我收到 NullRefernceException 错误,我正在观看一个视频系列,所以我也在做同样的事情,但无法 运行 code.He 正在使用 .Net 版本 =2.0 并且我正在使用 4.0.

这是我的母版页的代码

<asp:Label ID="HeadingLabel" runat="server" Font-Bold="True"Text="Online Shopping Website"></asp:Label>

看起来 Master 为空。如果是,则您所在的页面实际上并非基于母版页。

确保 aspx 实际上有一个在 @Page 指令中指定的母版页。