Hi, I have encountered an issue that must be common with Nested Master Pages. The problem I am having is when my content page uses a child master page, it calls the Master Page's Page_Load. Inside the Page_Load it is modifying a Literal that will not exist on the content page and throws an error. I understand why this happens, but my question is what is the best way to avoid this?
Site.master
Site.master.cs
Child.master
Site.master
Code:
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder id="ContentPlaceHolder2" runat="server">
<asp:Literal ID="Literal2" runat="server"></asp:Literal>
</asp:ContentPlaceHolder>
Site.master.cs
Code:
public partial class Site : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = "something"; // Works fine
Literal2.Text = "something"; // Error occurs here.
}
}
Child.master
Code:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<!-- No Literal Control ->
</asp:Content>