C# – Accessing user control on child master page from child master page code behind

asp.netcmaster-pages

Trying to set the value of a literal user control on my child master page via the code behind of the same master page.

Here is example of the code I am using:

Global.master

<form id="form1" runat="server">
<div>
    <asp:ContentPlaceHolder id="GlobalContentPlaceHolderBody" runat="server">

    </asp:ContentPlaceHolder>
</div>
</form>

Template.master (child of Global.master)

<asp:Content ID="TemplateContentBody" ContentPlaceHolderID="GlobalContentPlaceHolderBody" Runat="Server">
<asp:Literal ID="MyLiteral1" runat="Server"></asp:Literal>

<p>This is template sample content!</p>
  <asp:ContentPlaceHolder ID="TemplateContentPlaceHolderBody" runat="server">

</asp:ContentPlaceHolder>

Template.master.cs

    protected void Page_Load(object sender, EventArgs e)
{
    MyLiteral1.Text = "Test";
}

ContentPage.aspx

< asp:Content ID="ContentBody" ContentPlaceHolderID="TemplateContentPlaceHolderBody" Runat="Server">
</asp:Content>

Once I am able to achieve this, I will also need to be able to access content on the global and template master pages via content pages.

Best Answer

If I understand your scenario you want to have your content pages access items from your master pages. If so, you'll need to setup a property to expose them from your master page, and in your content page you can setup a MasterType directive.

Take a look at this post for an example.

Related Topic