Access Master Page public method from user control/class/page
Asked Answered
S

2

6

I am to access a method on my master page. I have an error label which I want to update based on error messages I get from my site.

public string ErrorText
{
    get { return this.infoLabel.Text; }
    set { this.infoLabel.Text = value; }
}

How can I access this from my user control or classes that I set up?

Sempiternal answered 25/10, 2010 at 9:38 Comment(0)
D
1

Page should contain next markup:

<%@ MasterType VirtualPath="~/Site.master" %>

then Page.Master will have not a type of MasterPage but your master page's type, i.e.:

public partial class MySiteMaster : MasterPage
{
    public string ErrorText { get; set; }
}

Page code-behind:

this.Master.ErrorText = ...;

Another way:

public interface IMyMasterPage
{
    string ErrorText { get; set; }
}

(put it to App_Code or better - into class library)

public partial class MySiteMaster : MasterPage, IMyMasterPage { }

Usage:

((IMyMasterPage )this.Page.Master).ErrorText = ...;
Doralynne answered 25/10, 2010 at 10:2 Comment(2)
Hey there abatischev, I'm having exact same issue as OP. I'm about to try the interface route, that looks swift :) Just curious, is MasterType, used in the first example valid in a User control? I know this works fine with child pages, wondering if there's a way to get this to work with user controls before implementing option 2. Thanks!Cloe
@Chris: Hi! As far as I know - unfortunately no, it doesn't work for UserControls. UC knows nothing about parent page's master page, just has a reference to them: this.Page, this.Page.Master, so option #2 is the only one I knowDoralynne
P
5

To access the masterpage:

this.Page.Master

then you might need to cast to the actual type of the master page so that you could get the ErrorText property or make your master page implement an interface containing this property.

Pancratium answered 25/10, 2010 at 9:43 Comment(2)
How can I use this.Page.Master from a class. It does not seem to work for me.Sempiternal
#417191Tindall
D
1

Page should contain next markup:

<%@ MasterType VirtualPath="~/Site.master" %>

then Page.Master will have not a type of MasterPage but your master page's type, i.e.:

public partial class MySiteMaster : MasterPage
{
    public string ErrorText { get; set; }
}

Page code-behind:

this.Master.ErrorText = ...;

Another way:

public interface IMyMasterPage
{
    string ErrorText { get; set; }
}

(put it to App_Code or better - into class library)

public partial class MySiteMaster : MasterPage, IMyMasterPage { }

Usage:

((IMyMasterPage )this.Page.Master).ErrorText = ...;
Doralynne answered 25/10, 2010 at 10:2 Comment(2)
Hey there abatischev, I'm having exact same issue as OP. I'm about to try the interface route, that looks swift :) Just curious, is MasterType, used in the first example valid in a User control? I know this works fine with child pages, wondering if there's a way to get this to work with user controls before implementing option 2. Thanks!Cloe
@Chris: Hi! As far as I know - unfortunately no, it doesn't work for UserControls. UC knows nothing about parent page's master page, just has a reference to them: this.Page, this.Page.Master, so option #2 is the only one I knowDoralynne

© 2022 - 2024 — McMap. All rights reserved.