我的目标是能够为子页面中的元素定义一个类。在我的母版页上,我有以下内容:
<body class="<myown:AttributePlaceHolder runat="server" ID="BodyCssClass"/>">
AttributePlaceHolder
派生自,ContentPlaceHolder
并且只是从其内容中去除任何换行符和多余的空格。我将描述的问题也发生在将 更改AttributePlaceHolder
为 normal时ContentPlaceHolder
。
现在上述代码段不起作用,最终会出现以下错误:
在母版页“/Views/Shared/Site.Master”中找不到 ContentPlaceHolder“BodyCssClass”,请在内容页中验证内容控件的 ContentPlaceHolderID 属性
将上述代码行更改为:
<body class="<% %><myown:AttributePlaceHolder runat="server" ID="BodyCssClass"/>">
它确实有效(注意添加的<% %>
)。
它可以解决问题,只是想知道我是否在这里遗漏了一些东西。
只是为了获得额外的信息,在我的子页面中我可以写:
<asp:Content ContentPlaceHolderID="BodyCssClass" runat="server">profile-edit someotherclass another-class</asp:Content>
甚至:
<asp:Content ContentPlaceHolderID="BodyCssClass" runat="server">profile-edit
someotherclass
another-class
</asp:Content>
它将被很好地打印为:
<body class="profile-edit someotherclass another-class">
编辑
正如约翰所指出的,以下也有效:
<body class='<myown:AttributePlaceHolder runat="server" ID="BodyCssClass"/>'>
将双引号变为单引号。
但是我的 html 也会出现单引号。说我疯了,但这只会伤害我...
我想这与 ASP.NET 解析引擎有关,在这种情况下,我们应该将其称为错误还是“功能”?