Call User Control Method in Master Page from Content Page

ぃ、小莉子 提交于 2019-12-24 10:49:58

问题


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 ?


回答1:


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()




回答2:


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();
}


来源:https://stackoverflow.com/questions/15019598/call-user-control-method-in-master-page-from-content-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!