How to reference a Master Page from a user control?(如何从用户控件引用母版页?)
问题描述
我正在寻找一种方法来(最好)从使用母版页的内容页中找到的用户控件中强键入母版页.
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;
您必须确保将正确的 using 语句添加到文件顶部,或内联限定母版页类型.
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.
这篇关于如何从用户控件引用母版页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从用户控件引用母版页?
基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
