简体   繁体   English

从内容页面调用母版页中的用户控制方法

[英]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 我的母版页中有一个自定义用户控件,可将站点范围的JavaScript库加载到母版页的头部

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 我想让使用MasterPage的页面从本质上调用用户控件MyScriptControl的方法

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 ? 或者有更好的方法吗?

Step 1: Add a MasterType directive in your aspx page 第1步:在aspx页面中添加MasterType指令
Step 2: Create a public method in your master page which will call your usercontrol method. 第2步:在母版页中创建一个公共方法,该方法将调用您的usercontrol方法。
Step 3: From your page you can call that method using Master.Method() 第3步:从您的页面,您可以使用Master.Method()调用该方法

You should use the MasterType directive in your page. 您应该在页面中使用MasterType指令。

Using MasterType, your page's Master property will be of the type of your master page, and not of the base class MasterPage . 使用MasterType,您的页面的Master属性将是您的母版页的类型,而不是基类MasterPage You should be able to do something like 你应该能够做类似的事情

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM