Seth Duncan
Seth Duncan

Reputation: 1255

Call User Control Method in Master Page from Content Page

I have a Custom User Control present in my master page that loads site-wide JavaScript libraries into the master page's head

Like so:

<html>
    <head>
        <CustomControl:MyScriptControl id="scripts" runat="server" />
    </head>

    <body>

        <asp:ContententPlaceHolder id="pageContent" runat="server" />

    </body>

</html>

I would like to have the page that uses the MasterPage call a method of the User Control MyScriptControl so essentially

public partial class ChildPage: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

          //// Invoke Method of MasterPage's UserControl: MyScriptControl

    }
}

Is this possible ? Or is there a better way to do this ?

Upvotes: 1

Views: 3808

Answers (2)

Pratik Mehta
Pratik Mehta

Reputation: 1352

Step 1: Add a MasterType directive in your aspx page
Step 2: Create a public method in your master page which will call your usercontrol method.
Step 3: From your page you can call that method using Master.Method()

Upvotes: 2

Paolo Tedesco
Paolo Tedesco

Reputation: 57202

You should use the MasterType directive in your page.

Using MasterType, your page's Master property will be of the type of your master page, and not of the base class MasterPage. You should be able to do something like

protected void Page_Load(object sender, EventArgs e) {
    Master.scripts.SomeMethod();
}

Upvotes: 1

Related Questions