[英]query regarding the master page in asp.net/C#/visual studio 2008
我想在母版頁的側邊欄中有4個鏈接,我的Web應用程序中的所有其他表單(內容頁)都繼承。
<table>
<tr>
<td width= "150px">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Menu runat="server" ID="MainMenu1" CssClass="MasterContent" StaticSubMenuIndent="30px">
<Items>
<asp:MenuItem Text="My Software" NavigateUrl="~/MySoftware.aspx"></asp:MenuItem>
<asp:MenuItem Text="Check Out" NavigateUrl="~/CheckOut.aspx"></asp:MenuItem>
<asp:MenuItem Text="View Shopping Cart" NavigateUrl="~/ShoppingCart.aspx"></asp:MenuItem>
<asp:MenuItem Text="Continue Shopping" NavigateUrl="~/Start.aspx"></asp:MenuItem>
</Items>
</asp:Menu>
</asp:ContentPlaceHolder>
</td>
<td width="900px">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
這是母版頁中的內容,您可以看到有4個菜單項,我試圖將其作為側邊欄形成,並且我試圖在home.aspx(其中一個內容頁)中進行繼承,如下所示:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Start.aspx.cs" Inherits="WebStore._Start"
MasterPageFile="~/Webstore.Master" %>
<asp:Content ID="StartSideBar" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
但是不幸的是,側邊欄根本沒有顯示。 我知道我在某些地方犯了根本性的錯誤。 我的目的是讓網頁有側邊欄像這樣 。 這是我想要的頁面的屏幕截圖。
有人可以指導我解決這個問題嗎?
謝謝你的期待
設置方式中,home.aspx文件破壞了菜單鏈接,因為它將ContentPlaceholder1的內容定義為空(或任何其他方式),這將覆蓋您放入MasterPage內部的菜單鏈接同一內容所有者。 母版頁和內容頁的工作方式是,母版頁為內容頁(例如home.aspx)定義了將內容加載到其中的位置(ContentPlaceholder)。 但是,您已經將要保留的實際內容放在MasterPage的ContentPlaceholder中-這意味着,為ContentPlaceholder1定義內容的任何內容頁面(同樣,如home.aspx)(例如,與StartSideBar一樣)都將覆蓋已定義的任何內容在MasterPage的ContentPlaceholder1內-在這種情況下,您的菜單鏈接。
如果希望菜單鏈接在每個內容頁面上保持不變,則應將其從MasterPage上的ContentPlaceholder1中移出,並使其僅在MasterPage上進行標記(實際上,您應該將ContentPlaceholder1一起刪除)。 一種簡單的方法是像這樣注釋掉ContentPlaceholder標記本身:
<table>
<tr>
<td width="150px">
<%--<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">--%>
<asp:Menu runat="server" ID="MainMenu1" CssClass="MasterContent" StaticSubMenuIndent="30px">
<Items>
<asp:MenuItem Text="My Software" NavigateUrl="~/MySoftware.aspx"></asp:MenuItem>
<asp:MenuItem Text="Check Out" NavigateUrl="~/CheckOut.aspx"></asp:MenuItem>
<asp:MenuItem Text="View Shopping Cart" NavigateUrl="~/ShoppingCart.aspx"></asp:MenuItem>
<asp:MenuItem Text="Continue Shopping" NavigateUrl="~/Start.aspx"></asp:MenuItem>
</Items>
</asp:Menu>
<%-- </asp:ContentPlaceHolder>--%>
</td>
<td width="900px">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</td>
</tr>
</table>
您還必須從內容頁面中刪除ContentPlaceholder引用,否則會收到一個錯誤提示(因為您是從母版中刪除它的,所以它在內容頁面中不存在-沒關系,現在是MasterPage有您想要的鏈接):
<%@ Page Title="" Language="C#" MasterPageFile="~/Webstore.master"
AutoEventWireup="true" CodeFile="Start.aspx.cs" Inherits="Start" %>
<%--<asp:Content ID="SideBarStart" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>--%>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.