2010-09-13 156 views
2

我試圖讓我的母版頁與我的內容頁面一起工作,允許內容頁面訪問母版頁控件。我得到的錯誤:ASP.net c#Masterpage問題

Parser Error Message: The 'mastertype' directive must have exactly one attribute: TypeName or VirtualPath

這是上線:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewProduct.aspx.cs" Inherits="AlphaPackSite.viewProduct" 
    MasterPageFile="MasterPages/Main.master" 
    title="Hi there!" 
%> 
<%@ MasterType TypeName="Main" VirtualPath="MasterPages/Main.master" %> 

我的母版頁:

namespace AlphaPackSite 
{ 
    public partial class Main : System.Web.UI.MasterPage 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

我有點困惑與命名空間和這樣做可能有讓他們錯了,但現在我相信所有頁面都在相同的名稱空間中。

更新

當我把它轉化爲:

<%@ MasterType VirtualPath="MasterPages/Main.master" %> 

我不斷收到錯誤:

Compiler Error Message: CS0030: Cannot convert type 'AlphaPackSite.Main' to 'ASP.masterpages_main_master' 

Source Error: 

Line 147:  public new ASP.masterpages_main_master Master { 
Line 148:   get { 
Line 149:    return ((ASP.masterpages_main_master)(base.Master)); 
Line 150:   } 
Line 151:  } 

回答

3

除非你想保留你的母版頁在當前頁面(在這個代碼被寫入)中的引用,否則我建議你刪除<%@ MasterType VirtualPath="MasterPages/Main.master" %> 行。

此行提供了一種訪問頁面主頁面的方法(例如,當您需要更改母版頁上的標籤或菜單需要添加更多項目時)。 如果主頁面的內容不需要您的內容頁面進行任何更改/更新,則不需要使用MasterType標籤。因爲通過使用MasterPageFile="MasterPages/Main.masterMasterType,您會混淆編譯器(即使主頁面是相同的)。

更新
如果你要保持中的MasterType標籤,然後從頁面指令的MasterPageFile="MasterPages/Main.master屬性。

+0

的'更新'標題下。這個答案是錯誤的。如果沒有MasterPageFile屬性,則不能使用MasterType指令,這會導致錯誤。 MasterType指令只允許您定義所使用的主控的類型,否則將始終默認爲System.Web.UI.MasterPage,並且您無權訪問繼承的類成員和方法,而無需每次調用​​都進行類型轉換。請參閱http://stackoverflow.com/questions/8946742/why-we-use-master-type – needfulthing 2017-04-03 12:37:13

0

嘗試:

<%@ MasterType TypeName="AlphaPackSite.Main" %> 
+0

感謝您的回答,因爲其他的答案,我已經更新的問題 – 2010-09-13 12:40:40

2

正如錯誤所述,@MasterType只需要一個參數。請試試:

<%@ MasterType VirtualPath="MasterPages/Main.master" %> 

請參閱http://msdn.microsoft.com/en-us/library/ms228274.aspx瞭解更多信息。

+0

謝謝你寫好了同樣的錯誤,我已經試過了已經和它拋出一個不同的錯誤:s – 2010-09-13 12:16:15

+0

它現在產生了哪個錯誤? – 2010-09-13 12:20:28

+0

我把它放在問題描述 – 2010-09-13 12:33:21

1

(1)註釋掉的MasterType指令和編譯網站 (2)取消註釋中的MasterType指令,並與任一類型的名稱或虛擬路徑放置,都將拋出錯誤

原因註釋和取消註釋: 從邏輯上講,當網站建立失敗時,它不會有 AlphaPackSite.Main創建,因此會拋出錯誤 但一旦你註釋掉了,並且代碼中沒有其他錯誤,你會希望得到你的箱子中的類型!

因此,有更多的機會與comment > compile > uncomment種事情的MasterType
參考工作: http://msdn.microsoft.com/en-us/library/ms228274.aspx

-1

我看到了這個問題,同時尋找其他的答案,想我會很快給出答案的情況下,其他人有這個問題。

問題是您不想在主名稱調用中使用虛擬路徑。相反,您需要使用TypeName併爲路徑添加下劃線。這是基於你的路徑上的正確標籤:

<%@ MasterType TypeName="AlphaPackSite_Main" %>