0

我的 asp.net 子母版页面设置如下:

<asp:Content ID="MainContent" runat="server" ContentPlaceHolderID="mainContent">
    <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">
    <html>
        <head >
            <title></title>
            <asp:ContentPlaceHolder ID="HeadContent" runat="server">
            </asp:ContentPlaceHolder>
        </head>
        <body>
            ...
        </body>
    </html>
</asp:Content>

为什么 ASP.NET 不断删除放在 HEAD 中的属性?具体来说,我试图让它像这样:

<head runat="server">

如果我在 HEAD 中没有 runat,我会收到以下错误:

Using themed css files requires a header control on the page. (e.g. <head runat="server" />). 

这是我的主控,它加载我上面的子母版页:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.Master.cs" Inherits="HeadOffice.Site" %>
<asp:contentplaceholder id="MainContent" runat="server">
</asp:contentplaceholder>

原来 asp:Content 正在从 html、head、body 中删除属性。不只是头。有没有办法阻止它?

4

3 回答 3

0

您的主主页看起来像这样?

 <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>
       <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

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

如果不使您的主母版页看起来像我提供给您的这段代码。在未加入的标签<link rel="stylesheet" type="text/css" href="my.css"/>之间添加 以避免删除它。<head><asp:ContentPlaceHolder ID="head" runat="server"/>

为什么您的 css 可以在每个回发中消失的另一种选择是aspx文件丢失MasterPagefileAutoEventWireup属性示例时的情况:

  <%@ Page Title="" Language="C#" MasterPageFile="~/Page.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>

编辑: 如果您的主要PageMaster<head>标签,您不能像在代码中那样在同一页面上添加另一个标签,您应该只添加内容

<asp:Content ID="content1" ContentPlaceHolderID="head" runat="server"/>放置在您的aspx页面上。

如果有帮助,请告诉我:)

于 2012-06-20T20:27:18.433 回答
0

我认为当您使用 runat=server 时,您需要给它一个 ID,这样服务器就会知道如何访问它

于 2012-06-20T20:27:48.443 回答
0

好吧,我发现的解决方法是,如果您触发 onSave 事件,asp:content 只会修改您的 HTML。所以你要做的就是进行编辑,然后不要保存,而是关闭 VS2010 程序,然后单击保存。这样,asp:content 将没有机会修改您的代码。之后,您可以重新打开该项目。

于 2012-06-21T14:59:11.790 回答