访问母版页的用户控件/班/页的公共方法控件、方法、用户、母版

2023-09-03 13:19:33 作者:厮守他、

我来访问的方法在我的母版页。我有我想根据我从我的网站获得错误信息来更新出错的标签。

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?

推荐答案

页应包含在下一个标记:

Page should contain next markup:

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

然后 Page.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; }
}

页code-背后:

Page code-behind:

this.Master.ErrorText = ...;

另一种方式:

Another way:

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

(把它APP_ code或更好 - 到类库)

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

public partial class MySiteMaster : MasterPage, IMyMasterPage { }

用法:

((IMyMasterPage )this.Page.Master).ErrorText = ...;