jpshook
jpshook

Reputation: 4944

Accessing user control on child master page from child master page code behind

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.

Upvotes: 2

Views: 4581

Answers (3)

Ahmad Mageed
Ahmad Mageed

Reputation: 96507

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.

Upvotes: 3

Chris
Chris

Reputation: 2055

EDIT: This is my answer when I thought you were trying to access a control on the child master from the parent master code behind.

You can use a recursive findControl function:

protected Control FindControlRecursive(string id, Control parent)
{
    // If parent is the control we're looking for, return it
    if (string.Compare(parent.ID, id, true) == 0)
        return parent;

    // Search through children
    foreach (Control child in parent.Controls)
    {
        Control match = FindControlRecursive(id, child);

        if (match != null)
            return match;
    }

    // If we reach here then no control with id was found
    return null;
}

Then use this code in your master page:

protected void Page_Load(object sender, EventArgs e)
{
//EDIT: if GlobalContentPlaceHolderBody isn't visible here, use this instead:
//Control c = FindControlRecursive("MyLiteral1", Page.FindControl("GlobalContentPlaceHolderBody"));
    Control c = FindControlRecursive("MyLiteral1", GlobalContentPlaceHolderBody);
    if(c != null)
        ((Literal)c).Text = "Test";
}

Upvotes: 0

jpshook
jpshook

Reputation: 4944

Found a working solution. In the template.master (nested child master), I had to put the code in OnLoad event.

protected override void OnLoad(EventArgs e)
{
    MyLiteral1.Text= "<p>MyLiteral1 Successfully updated from nested template!</p>";
    base.OnLoad(e);
}

Very strange...

Basically, I am using the global master as the page that has code shared on every page, then I will have various nested pages to suit each website section. For the navigation nested template, I want to be able to show if the user is logged in and how many items in shopping cart.

If there is a better way to achieve this, I am open to suggestions.

Upvotes: 1

Related Questions