Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET: Call function in MasterPage through UserControl

Calling a function from the MasterPage in a Page is quite straigt forward but how do I call it for a UserControl:

Adding <%@ MasterType VirtualPath="~/MasterPage.master" %>, doesn't work on UserControls.

So this.Page.Master.MyFunction() fails :(

like image 932
Niels Bosma Avatar asked Sep 18 '09 12:09

Niels Bosma


1 Answers

You have to cast the this.Page.Master first as the Master property of the Page is of type System.Web.UI.MasterPage.

e.g.

((MyMaster)this.Page.Master).MyFunction();

You could check the type of the underlying master page by adding a property to the code behind of the user control:

    public string MType
    {
        get { return this.Page.Master.GetType().FullName; }
    }

and print the result out in the User control markup, e.g. add this line to make it print out as a comment in the source code:

<!-- <%= MType %> //-->
like image 75
JDunkerley Avatar answered Sep 28 '22 01:09

JDunkerley