Resumé

Retrieve Master Page Resources from a UserControl

Cristian Merighi () 0.00

Quick howto. Retrieving Master page exposed resources from either nested Page or ASCX User Control.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

How to access Master page resources (properties/controls...) from nested elements such pages and/or user controls?
This article may help to accomplish this task.

Master page code key-lines (pay attention to the bolded ClassName attribute):

<%@ Master Language="C#" ClassName="TestMaster" %> 
<script runat="server"> 

    public string BodyCssClass
    {
        get { return "FOO"; }
    }
    
    void Page_Init(object o, EventArgs e)
    {
        mybody.Attributes["class"] = BodyCssClass;
    }
    
    
</script> 

<body id="mybody" runat="server"> 
 

Most important step to make the whole thing working is to add a @ Reference directive to your Page/UserControl code files so that the runtime can dynamically compile the referenced object (which is associated to a build provider).

Let's see some ASCX code lines:

<%@ Control Language="C#" ClassName="Test" %> 
<%@ Reference Control="Test.master" %> 
<script runat="server"> 

    void Page_Load(object o, EventArgs e)
    {
        Label1.Text = ((TestMaster)Page.Master).BodyCssClass; // @ Reference directive related.
        Label2.Text = ((HtmlGenericControl)Page.Master.FindControl("mybody")).Attributes["class"];
    }
    
</script> 

Working example:

zip file « download .zip file

Take care. Bye.

feedback
 

Syndicate

Author

Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated

Archive

Where am I?

Author

Cristian Merighi facebook twitter google+ youtube

I'm now reading

Feeds