简体   繁体   English

ASP.NET-如何在后面的页面代码中获取和设置MasterPage属性?

[英]ASP.NET - How to get and set MasterPage attribute in page code behind?

I have an ASP.NET page that uses a MasterPage. 我有一个使用MasterPage的ASP.NET页面。 I have a public bool attribute in the MasterPage called ShowChangePassword . 我在MasterPage中有一个public bool属性,称为ShowChangePassword I want to set this to true or false in the page that uses this MasterPage. 我想在使用此MasterPage的页面中将其设置为true或false。

I have the following code, but I can't set the attribute: 我有以下代码,但无法设置属性:

 var masterPage = this.Master;
 masterPage.ShowChangePassword = false;

What code do I need to do this? 我需要什么代码?

You need a typed reference to your master page class. 您需要对母版页类的类型化引用。 Put something like that in your content page: 在您的内容页面中放入类似的内容:

<%@ Page  masterPageFile="~/MasterPage.master"%>
<%@ MasterType  virtualPath="~/MasterPage.master"%> 

Then you can do: 然后,您可以执行以下操作:

 this.Master.ShowChangePassword = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM