如何从用户控件引用母版页?

时间:2022-10-19 22:23:34

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.

我试图从用户控件访问母版页的属性,而不需要将主页面的属性传递给用户控件的内容页,因为多个内容页面使用相同的用户控件。一个变化,一个地方什么的。

3 个解决方案

#1


18  

Try Page.Master.

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.

您必须确保将适当的using语句添加到文件的顶部,或者将主页类型限定为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.

一个可能的问题是,如果这个控件被一个主页面不是相同类型的其他页面使用。这只会在运行时被捕获。

#2


0  

Have you tryed Page.FindControl("name") on the usercontrol?

您是否在usercontrol上tryed Page.FindControl(“name”)?

#3


0  

The best way to do it that I've found is actually to build a custom class that is based off of UserControl, give it a Master property with a get accessor that fishes through the this.Page.Parent until it stops encountering master pages (If you are nesting, this step is unnecessary otherwise) and then return that web control as the type of the master page you want to use. Then, when you add a new user control, change it's base class to the name of your custom class. The .Master property will be accessible and cast properly as the master page you want it to use.

我发现最好的方法是构建一个基于UserControl的自定义类,给它一个带有get访问器的Master属性,通过this.Page进行访问。直到它停止遇到母页(如果您正在嵌套,则不需要执行此步骤),然后返回该web控件作为您想要使用的母页的类型。然后,在添加新用户控件时,将其基类更改为自定义类的名称。主属性将被访问并正确地转换为您希望它使用的主页面。

#1


18  

Try Page.Master.

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.

您必须确保将适当的using语句添加到文件的顶部,或者将主页类型限定为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.

一个可能的问题是,如果这个控件被一个主页面不是相同类型的其他页面使用。这只会在运行时被捕获。

#2


0  

Have you tryed Page.FindControl("name") on the usercontrol?

您是否在usercontrol上tryed Page.FindControl(“name”)?

#3


0  

The best way to do it that I've found is actually to build a custom class that is based off of UserControl, give it a Master property with a get accessor that fishes through the this.Page.Parent until it stops encountering master pages (If you are nesting, this step is unnecessary otherwise) and then return that web control as the type of the master page you want to use. Then, when you add a new user control, change it's base class to the name of your custom class. The .Master property will be accessible and cast properly as the master page you want it to use.

我发现最好的方法是构建一个基于UserControl的自定义类,给它一个带有get访问器的Master属性,通过this.Page进行访问。直到它停止遇到母页(如果您正在嵌套,则不需要执行此步骤),然后返回该web控件作为您想要使用的母页的类型。然后,在添加新用户控件时,将其基类更改为自定义类的名称。主属性将被访问并正确地转换为您希望它使用的主页面。