【问题标题】:Call User Control Method in Master Page from Content Page从内容页面调用母版页中的用户控制方法
【发布时间】:2013-02-22 19:53:49
【问题描述】:

我的母版页中有一个自定义用户控件,它将站点范围的 JavaScript 库加载到母版页的头部

像这样:

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

    <body>

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

    </body>

</html>

我想让使用 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

    }
}

这可能吗?或者有更好的方法吗?

【问题讨论】:

    标签: c# asp.net .net master-pages


    【解决方案1】:

    第 1 步:在您的 aspx 页面中添加 MasterType 指令
    第 2 步:在您的母版页中创建一个公共方法,该方法将调用您的 usercontrol 方法。
    第 3 步:在您的页面中,您可以使用 Master.Method() 调用该方法

    【讨论】:

      【解决方案2】:

      您应该在您的页面中使用MasterType 指令。

      使用 MasterType,页面的 Master 属性将是母版页的类型,而不是基类 MasterPage。你应该能够做类似的事情

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多