访问母版页的用户控件/班/页的公共方法 [英] Access Master Page public method from user control/class/page

查看:139
本文介绍了访问母版页的用户控件/班/页的公共方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 = ...;

这篇关于访问母版页的用户控件/班/页的公共方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆