2012-02-17 73 views
0

我有一個使用MasterPage的ASP.NET頁面。我在名爲ShowChangePassword的MasterPage中有publicbool屬性。我想在使用此MasterPage的頁面中將其設置爲true或false。ASP.NET - 如何在頁面代碼中獲取並設置MasterPage屬性?

我有下面的代碼,但我不能設置屬性:

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

什麼代碼,我需要做到這一點?

回答

2

您需要對您的母版頁類進行類型化引用。把類似的東西在你的內容頁:

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

然後,你可以這樣做:

this.Master.ShowChangePassword = false; 
+0

我試過,但沒有奏效 - 母版不具有<%@ Page指令。你的意思是把兩個服務器標籤放在主頁面上嗎?爲什麼要引用主頁面中的masterPageFile> – Theomax 2012-02-17 13:41:06

+0

@aspdotnetuser:必須將MasterType標記放入要引用母版頁的ContentPage中。 – Mithrandir 2012-02-17 13:43:02

相關問題