我的母版页包含以下内容:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Label ID="User" runat="server" Text="Welcome, " Visible="false"></asp:Label>
</asp:ContentPlaceHolder>
我正在尝试像这样访问标签:
Label welcomeLabel;
ContentPlaceHolder cPlaceHolder;
cPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
if (cPlaceHolder != null)
{
welcomeLabel = (Label)cPlaceHolder.FindControl("User");
if (welcomeLabel != null)
{
welcomeLabel.Text = "Welcome, " + u.Name;
welcomeLabel.Visible = true;
}
}
我已经检查过 ContentPlaceHolder1 确实找到了,并且在它上面使用 HasControls() 返回 true,但我似乎无法访问标签。
帮我?