1

我的目标是能够为子页面中的元素定义一个类。在我的母版页上,我有以下内容:

<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 解析引擎有关,在这种情况下,我们应该将其称为错误还是“功能”?

4

3 回答 3

1

尝试这样的事情:

<body class='<asp:ContentPlaceHolder id="PlaceHolderTitleAreaClass" runat="server">ms-areaseparator</asp:ContentPlaceHolder>' ... >
于 2009-06-11T08:42:38.607 回答
1

您还可以通过反转引号设置来实现输出中的双引号,而无需使用服务器标签:

<body class="<myown:AttributePlaceHolder runat='server' ID='BodyCssClass'/>">

The runat and ID attributes of the place holder are single quoted. As to why the server tag makes the original code work, only the demons inside the parsing engine know that...

于 2009-10-20T17:01:31.557 回答
0

你有没有想过这个?

在您的 aspx 页面中,将 body 标记设置为服务器对象

<body runat="server" id="HtmlBody">

然后在你后面的代码中你可以设置你喜欢的任何属性

HtmlBody.Attributes.Add("class", "your-css-class-name");

产生以下标记

<body id="ctl00_HtmlBody" class="your-css-class-name">
于 2009-06-11T13:03:39.690 回答