有关 asp.net/C#/Visual Studio 2008 中母版页的查询

发布于 2024-10-30 02:35:36 字数 1802 浏览 0 评论 0原文

我希望在母版页的侧栏中有 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">

但不幸的是侧边栏根本没有显示。我知道我在某些地方做错了。我的目的是让网页有像 this 这样的侧边栏。这是我想要的页面的屏幕截图。

有人可以指导我完成这个任务吗?

感谢期待

I want to have 4 links in the sidebar of master page which every other form (content page) in my web application inherits.

<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>

This is the content in the master page as you can see there are 4 menu items i tried to form as sidebar and i tried to inherit in home.aspx(one of the content pages) as follows:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Start.aspx.cs" Inherits="WebStore._Start"
MasterPageFile="~/Webstore.Master" %>
<asp:Content ID="StartSideBar" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">

But unfortunately the sidebar is not at all getting displayed. I know i'm doing fundamentally wrong some where. My intention is to have web pages to have sidebar like this. It is a screenshot of my intended page.

Can some one guide me through this.

Thanks in anticipation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

决绝 2024-11-06 02:35:36

按照您的设置方式,您的 home.aspx 文件正在破坏您的菜单链接,因为它将 ContentPlaceholder1 的内容定义为空(或无论如何其他内容),这会覆盖您在 MasterPage 中放置的菜单链接相同的内容持有者。 MasterPage 和内容页的工作方式是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 引用,否则您将收到编译错误(因为您从 Master 中删除了它,它不能存在于内容页面中 - 但没关系,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>--%>

The way you have it set up, your home.aspx file is clobbering your menu links because it is defining the content of ContentPlaceholder1 to be empty (or something else at any rate), which is overriding the menu links you put in your MasterPage inside the same content holder. The way MasterPages and content pages work is that the MasterPage defines a location (a ContentPlaceholder) for the content page (e.g., home.aspx) to load content into. But you have put actual content you want to keep inside a ContentPlaceholder on your MasterPage--which means that any content page (again, like home.aspx) that defines content for ContentPlaceholder1 (like you do with StartSideBar) is going to override anything defined inside ContentPlaceholder1 on the MasterPage--in this case, your menu links.

If you want your menu links to remain constant on every content page, then you should move them out of ContentPlaceholder1 on the MasterPage and make them just markup on the MasterPage (in fact, you should probably remove ContentPlaceholder1 altogether). The easy way to do this is to comment out the ContentPlaceholder tag itself like this:

<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>

You will also have to remove the ContentPlaceholder reference from your content page as well, or you will get a compliation error (because you removed it from the Master, it can't exist in the content page--but that's okay, the MasterPage now has the links you want):

<%@ 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>--%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文