如何创建创建母版页变量/属性,让子页面访问它们? [英] How to create create variables/properties in master page, and let sub-pages access them?

查看:172
本文介绍了如何创建创建母版页变量/属性,让子页面访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建创建母版页变量/属性,让子页面访问它们?

How to create create variables/properties in master page, and let sub-pages access them?

所以,我的主人将有一个字符串属性HomeUrl

So my master will have a string property HomeUrl

如何才能使用该母版页访问此属性的任何页面?

How can any page that uses the master page access this property?

推荐答案

您应该为你的母版页,这将定义您属性中使用的基类:

You should use a base class for you master page which will define you properties:

public class BaseMasterPage : System.Web.UI.MasterPage
{
    public string HomeUrl {get; set; }
}

和那么你的母版页应该从这个 BaseMasterPage 类继承(为例):

And then your master page should inherit from this BaseMasterPage class (as example):

// real master page
public partial class Common_MasterPages_Backend_Default : BaseMasterPage
{
}

这之后,你可以通过 Page.Master 访问你的财产属性:

After this you can access your property through Page.Master property:

BaseMasterPage baseMaster = (BaseMasterPage)Page.Master;
string homeUrl = baseMaster.HomeUrl;

从使用该母版页。任何页面

from any page which uses this master page.

这篇关于如何创建创建母版页变量/属性,让子页面访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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