项目点击时来自contentplaceholder的项目消失

时间:2014-10-01 12:34:01

标签: c# html asp.net master-pages

我正在创建一个母版页,其中有两个ContentPlaceHolders。 ContentPlaceHolders1用于存在超链接的菜单,而在其他ContentPlaceHolders(即ContentPlaceHolders3)中,选定的超链接将打开其页面。问题是左边的菜单显示,但是当我点击其中的一个超链接时,页面在ContentPlaceHolders3中打开,但是来自ContentPlaceHolders1的整个菜单消失了。 picture.aspx和sample.aspx是要在ContentPlaceHolder3中打开的页面。

这是不同页面的代码:

admin_panel.master:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<body>

<form id="form1" runat="server">

        <table style="font-family: Arial">
            <tr>
                <td colspan="2" style="width: 800px; height: 80px; background-color: #3399FF; text-align: center">
                    <h1>WebSite Header</h1>
                </td>
            </tr>
            <tr>
                <td style="height: 500px; background-color: #66FFFF; width: 150px">
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

            </asp:ContentPlaceHolder>
                </td>
                <td style="height: 500px; background-color: #9999FF; width: 650px">
                    <h3>Content</h3>
                    <div>
                        <asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server">
                        </asp:ContentPlaceHolder>
                    </div>
                </td>
            </tr>

        </table>    
</form>
</body>
</html>

pictures.aspx:

    <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="pictures.aspx.cs" Inherits="pictures" MasterPageFile="~/admin_panel.master"%>


        <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">

            <asp:Image ID="Image1" runat="server" Height="200px" />
            <asp:Image ID="Image2" runat="server" Height="200px" />
            <asp:Image ID="Image3" runat="server" Height="200px" /><br />
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

        </asp:Content>

样品:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sample.aspx.cs" Inherits="sample"  MasterPageFile="~/admin_panel.master"%>

    <asp:Content ID="nil" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">

        <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" style="height: 26px" />
    </div>

    </asp:Content>

admin.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/admin_panel.master" AutoEventWireup="true" CodeFile="admin.aspx.cs" Inherits="admin" %>


<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server" >
    <asp:HyperLink ID="HyperLink1" NavigateUrl="~/pictures.aspx" runat="server">Click Me</asp:HyperLink><br />
    <asp:HyperLink ID="HyperLink2" NavigateUrl="~/sample.aspx" runat="server">Click Me 2...</asp:HyperLink>
</asp:Content>

1 个答案:

答案 0 :(得分:0)

原因是当你打开页面sample.aspx时,你得到的是Master Page + Sample Page的组合。所以我们没有定义ContentPlaceHolder1内容。即不在sample.aspx中而不在admin_panel.master中。这就是为什么它是空白的原因。您可以将其放在母版页中,而不是在管理员中指定菜单。像:

   <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
     <asp:HyperLink ID="HyperLink1" NavigateUrl="~/pictures.aspx" runat="server">Click Me</asp:HyperLink><br />
     <asp:HyperLink ID="HyperLink2" NavigateUrl="~/sample.aspx" runat="server">Click Me 2...         </asp:HyperLink>
   </asp:ContentPlaceHolder>

那应该可以解决你的问题。 :)