views:

343

answers:

3

Hi,

My goal is to be able to define a class for the element in a child page. On my master page I have the following:

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

The AttributePlaceHolder derives from ContentPlaceHolder, and just strips out any linebreaks and extra spaces from its content. The problem I will describe also occurs when changing the AttributePlaceHolder into a normal ContentPlaceHolder .

Now the above stated piece of code doesn't work, and will end up in the following error:

Cannot find ContentPlaceHolder 'BodyCssClass' in the master page '/Views/Shared/Site.Master', verify content control's ContentPlaceHolderID attribute in the content page

When changing the above line of code into:

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

It does work (note the added <% %>).

It does the trick, just wondering if I'm missing something here.

Just for the extra info, in my child pages I can write:

<asp:Content ContentPlaceHolderID="BodyCssClass" runat="server">profile-edit someotherclass another-class</asp:Content>

or even:

    <asp:Content ContentPlaceHolderID="BodyCssClass" runat="server">profile-edit
someotherclass

another-class
</asp:Content>

and it will be nicely printed out as:

<body class="profile-edit someotherclass another-class">

Edit
As pointed out by Johan the following also works:

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

Changes the double quotation marks into single ones.

But than my html would also show up with single quotation marks. Call me crazy, but that just hurts me...

I guess it has to do with the ASP.NET parsing engine, in that case should we call it a bug, or a "feature"?

A: 

try something like this:

<body class='<asp:ContentPlaceHolder id="PlaceHolderTitleAreaClass" runat="server">ms-areaseparator</asp:ContentPlaceHolder>' ... >
Johan Leino
I added your answer into my update.
Gidon
A: 

Have you thought about this?

In your aspx page have set the body tag to be a server object

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

Then in your code behind you can set any attributes you like

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

Which produces the following markup

<body id="ctl00_HtmlBody" class="your-css-class-name">
Kane
Although I did not state it in the text, I did tag it as MVC. So I don't have code behind, and don't want to set the class name in the controller. The class name should be set in the view.
Gidon
+1  A: 

You can also achieve the double quotation marks in the output without using the server tags by reversing the quotes setup:

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

TSmith