2013-05-14 73 views
1

我想在asp.net中添加一個masterpage,但是我收到一個錯誤消息,指出該頁面包含的標記在連接到masterpage時無效。這裏是我的母版文件:在asp.net中獲取masterpage錯誤

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" 
Inherits="Pages_AdminMaster" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
<asp:ContentPlaceHolder id="head" runat="server"> 
</asp:ContentPlaceHolder> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div style="text-align: right"> 
    <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server"> 

     <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Log Out</asp:LinkButton> 

    </asp:ContentPlaceHolder> 
</div> 
</form> 

這裏是網頁形式,我申請的是母版:

<%@ Page Language="C#" MasterPageFile="~/Pages/MasterPage.master" 
AutoEventWireup="true" CodeFile="Admin.aspx.cs" Inherits="Pages_Admin" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title></title> 
<style type="text/css"> 
    .style1 
    { 
     text-decoration: underline; 
     color: #0000FF; 
    } 
    .style2 
    { 
     font-family: 15; 
     font-size: 12pt; 
     color: #0000FF; 
    } 
</style> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div class="style2" style="text-align: right"> 

</div> 
</form> 
<p class="style1"> 
    <a href="CreateCourse.aspx">Create a new course</a></p> 
<p class="style1"> 
    <a href="ModifyCourse.aspx">Modify a course</a></p> 
<p class="style1"> 
    <a href="CreateAccount.aspx">Create a new account</a></p> 
<p class="style1"> 
    <a href="DeleteAccount.aspx">Delete an account</a></p> 
<p class="style1"> 
    <a href="DisableAccount.aspx">Disable an Account</a></p> 
<p class="style1"> 
    <a href="DropStudent.aspx">Drop a Student from a course</a></p> 
<p class="style1"> 
    <a href="EnrollStudent.aspx">Enroll a student to a course</a></p> 
<p class="style1"> 
    <a href="GetStudentInfo.aspx">Get Student info</a></p> 
<p class="style1"> 
    <a href="GetInstructorInfo.aspx">Get Instructor info</a></p> 
</body> 
</html> 

這裏是在瀏覽器中的錯誤:

Server Error in '/Bannerweb' Application. 

Content controls have to be top-level controls in a content page or a nested master 
page that references a master page. 

Description: An unhandled exception occurred during the execution of the current web 
request. Please review the stack trace for more information about the error and where 
it 
originated in the code. 

Exception Details: System.Web.HttpException: Content controls have to be top-level 
controls in a content page or a nested master page that references a master page. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. 
Information regarding the origin and location of the exception can be identified using 
the 
exception stack trace below. 

Stack Trace: 


[HttpException (0x80004005): Content controls have to be top-level controls in a 
content page or a nested master page that references a master page.] 
    System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, 
VirtualPath masterPageFile, IDictionary contentTemplateCollection) +8968123 
    System.Web.UI.Page.get_Master() +54 
    System.Web.UI.Page.ApplyMasterPage() +15 
    System.Web.UI.Page.PerformPreInit() +45 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean  
includeStagesAfterAsyncPoint) +328 

這兩個頁面都位於名爲Pages的文件夾中。誰能幫我這個?謝謝。

回答

2

使用母版頁的網絡表單,您需要將您的內容放置在<asp:Content ContentPlaceholderID="ContentPlaceHolder1" runat="server"></asp:Content>之內,該網頁將內容映射到母版頁中某個頁面上顯示的內容。

MSDN有關於如何在母版頁上使用<asp:ContentPlaceHolder /><asp:Content />http://msdn.microsoft.com/en-us/library/wtxbf3hh(v=vs.100).aspx的更多信息。

+0

感謝您的回答,但實際上我不明白該怎麼做 – yrazlik 2013-05-14 16:04:47

+0

您需要用標籤來包裝你的網絡表單。按照Steven提到的鏈接。這很好解釋。 – rivarolle 2013-05-14 16:23:55

1

當您在Visual Studio中「添加新項目」時,您應該選擇「Web Form」,然後在窗口的右下角,在您填寫的「名稱」框右側,檢查表示「選擇母版頁」的框。

這樣做會給你另一個對話框,點擊「添加」後,選擇新的Web窗體應該使用哪個主頁面。

您的新網絡表格將包含<Content>標記 - 通常用於標題和內容;取決於您的母版頁。網絡表格將不會有所有常見的<doctype><head>標籤等。

而不是試圖屠宰您當前的頁面以獲得正確的架構,我會開始一個新的,只需要引入你需要的代碼。

0

儘管MSDN已經很好地解釋了這種情況,即使您已將代碼放入<asp:content>標記中,您也可能會得到相同的錯誤。

檢查,如果你已經使用**<B> </B>**標籤的地方<TD> </TD>標籤中有它會顯示錯誤消息,將其轉換爲小<b> </b>標籤,你將能夠看到設計的一部分。

感謝