如何从一个用户控件引用一个母版页? [英] How to reference a Master Page from a user control?

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

问题描述

我正在寻找一种方式来(preferably)强烈建议这是在使用该母版页内容页找到用户控件输入一个母版页。

I'm looking for a way to (preferably) strongly type a master page from a user control which is found in a content page that uses the master page.

不幸的是,你不能在用户控制使用这样的:

Sadly, you can't use this in a user control:

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

我试图来访问用户控件的母版页的属性,宁可不要从母版页属性传递给内容页面,用户控件,因为多个内容页使用相同的用户控件。一个变化,一个地方,诸如此类的东西。

I'm trying to access a property of the master page from the user control and would rather not have to pass the property from the master page to the content page to the user control because multiple content pages use the same user control. One change, one place whatnot.

推荐答案

尝试 Page.Master

Whatever whatev = (Whatever)Page.Master;

你必须确保你添加适当的使用语句到文件的顶部,或限定母版页内嵌式

You'll have to make sure you add the proper using statements to the top of your file, or qualify the Master page type inline.

一个潜在的疑难杂症是,如果此控件使用的母版页是不一样的类型不同的网页。这只会在运行时获取抓住。

One potential gotcha is if this control is used by a different page whose master page is NOT the same type. This would only get caught at runtime.

这篇关于如何从一个用户控件引用一个母版页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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