Why can't I use a ContentPlaceholder inside HTML attributes in ASP.NET?
I'm using ASP.NET MVC2 and C#, but this question applies to ASP.NET in general.
This breaks:
<body id="<asp:ContentPlaceHolder ID="BodyID' runat="server" />">
Intellisense underlines the body
tag and the opening quote immediately after id=
, and complains:
Validation (HTML 4.01): Element 'body' is missing the '>' character from its start tag.
The asp element is ignored, and the id attribute is empty in the rendered HTML. (Same problem whether I use double or single quotes inside the ASP element, tho the latter breaks syntax hilighting in VS.)
This works (assuming I set the session variable):
<body id="<%: Session["BodyID"] %>">
Why is it that inline evaluation is supported inside HTML attributes, but ASP controls won't render inside attributes?
Here's my use case: based on data passed from the controller, the view knows what type of data it's rendering. The view injects data into various places in the master page. I can inject a title into the head, and markup into the body -- but I also want to inject data into some attributres. IDs and class names are the obvious examples, but there are others.
I want to do this while still maintaining valid markup; no tricks like dynamically rendering the entire body tag -- I want a page that looks like a valid HTML or XML doc at all times in Visual Studio.
Using inline eval is OK, but it requires me to set properties, which I do in the model or controller. In some cases that's necessary, but in others the values are static -- I have a view that's purpose-built, and I just need to inject a static value from the view into the master page. I don't want to go thru all the overhead of creating an abstract controller class, having all my controllers inherit from it, etc. just to get to the same functionality I already have when I'm injecting markup.
Side question (yes, I should open a separate question for it): What's the order of evaluation of ASP controls and inline code nuggets? I assume the code nuggets are resolved first, before the ASP controls, so I could e.g. put a code nugget inside an ASP control declaration. But I can't find docs that detail the process -- can anyone point them out to me?
Thanks!
UPDATE: Pauli mentioned that you can indeed use ContentPlaceholders anywhere you like, as long as they're not inside an element that's already marked runat="server"
. I tested again, and see he's correct -- I missed that initially. Visual Studio is still confused and gives an H开发者_JAVA百科TML validation warning, but the expected value appears in the attribute when the page is rendered. So, the answer to the question is "But you can!"
- you shouldn't use WebForms controls with MVC
- use '' around your ID and runat, since the parser is confused of all your conflicting "'s
Since the body tag doesn't have runat="server" its not treated as a server control, but just clear text and you can therefor put a contentplaceholder, or whatever other control wherever you want.
精彩评论